Gib*_*boK 6 linq entity-framework entity-framework-4
我使用EF 4,我DateTimeStart在我的实体中有一个属性,在这种格式中有date 16/08/2012 08:14:40,我想用EF查询并查找所有实体within the date 16/08/2012 only.使用下面代码我收到此错误
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Run Code Online (Sandbox Code Playgroud)
我的代码
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateNow = dateTimeNow.Date;
return db.EventCustoms.Where(x => x.DataTimeStart.Date <= dateNow)
.Select(y => new { y.EventId, y.EventTitle, y.DataTimeStart });
Run Code Online (Sandbox Code Playgroud)
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateTomorrow = dateTimeNow.Date.AddDays(1);
return db.EventCustoms.Where(x => x.DataTimeStart < dateTomorrow)
.Select(y => new { y.EventId, y.EventTitle, y.DataTimeStart });
Run Code Online (Sandbox Code Playgroud)
[编辑] @GibboK,详细说明:
实体框架无法在数据库端转换DateTime对象上的Date属性.
你的选择是:
(1)(如上所述)重新考虑您的查询并尝试在表中的每一行不需要在数据库端进行函数调用的解决方案....这对查询性能也有好处
(2)或者如果不可能,您可以使用EntityFunctions类,该类公开可以由EF转换为基础数据源的相应本机函数的方法(例如TruncateTime).
例如
return db.EventCustoms
.Where(x => EntityFunctions.TruncateTime(x.DataTimeStart) <= dateNow)
Run Code Online (Sandbox Code Playgroud)