为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ibeautiful 2019-11-12 10:15:44
在执行以下语句时出现异常。 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.我知道例外的含义,但我不知道如何摆脱它。有什么帮助吗?
查看完整描述

3 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

您可以使用EntityFunctions的TruncateTime方法将属性正确转换为SQL:Date


using System.Data.Objects; // you need this namespace for EntityFunctions


// ...


DateTime ruleData = Convert.ToDateTime(rule.data).Date;

return jobdescriptions

    .Where(j => EntityFunctions.TruncateTime(j.JobDeadline) == ruleData);


更新: EntityFunctions在EF6中已弃用,使用DbFunctions.TruncateTime


查看完整回答
反对 回复 2019-11-12
  • 3 回答
  • 0 关注
  • 1438 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信