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

标签 entity-framework linq-to-entities

我在执行以下语句时遇到异常。

 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.

我知道异常意味着什么,但我不知道如何摆脱它。有什么帮助吗?

最佳答案

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

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

关于entity-framework - LINQ to Entities 异常中不支持指定的类型成员 'Date',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11597373/

相关文章:

c# - 在单独的数据访问和业务逻辑层中,我可以在业务层中使用 Entity Framework 类吗?

c# - 一对一关系首先在 Entity Framework 代码中失败

c# - 动态添加新的 lambda 表达式以创建过滤器

c# - 在 LINQ to entities 中使用返回 IQueryable 的自定义方法

entity-framework - 如何重新创建我的EF代码第一表?

c# - 尝试为 StudentId=1 获取 "Grade"时出错

c# - MVC C# 奇怪的 NullReferenceException

c# - 使用 linq 检查 ILookup<string, string> 中的键值是否存在的最佳方法

linq-to-entities - 如何制作 "Except"LINQ to Entities查询?

c# - 将 DateTime.Add(TimeSpan) 与 LINQ 结合使用