猿问

LINQ to Entities异常不支持指定的类型成员'Date'

在执行以下语句时出现异常。


 DateTime result;

 if (!DateTime.TryParse(rule.data, out result))

     return jobdescriptions;

 if (result < new DateTime(1754, 1, 1)) // sql can't handle dates before 1-1-1753

     return jobdescriptions;

 return jobdescriptions.Where(j => j.JobDeadline.Date == Convert.ToDateTime(rule.data).Date );

例外


The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

我知道例外的含义,但我不知道如何摆脱它。有什么帮助吗?


ibeautiful
浏览 1495回答 3
3回答

长风秋雁

您可以使用EntityFunctions的TruncateTime方法将属性正确转换为SQL:Dateusing System.Data.Objects; // you need this namespace for EntityFunctions// ...DateTime ruleData = Convert.ToDateTime(rule.data).Date;return jobdescriptions&nbsp; &nbsp; .Where(j => EntityFunctions.TruncateTime(j.JobDeadline) == ruleData);更新: EntityFunctions在EF6中已弃用,使用DbFunctions.TruncateTime
随时随地看视频慕课网APP
我要回答