Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq EF如何按日期筛选数据_Linq_Entity Framework_Entity Framework 4 - Fatal编程技术网

Linq EF如何按日期筛选数据

Linq EF如何按日期筛选数据,linq,entity-framework,entity-framework-4,Linq,Entity Framework,Entity Framework 4,我使用EF 4,在我的实体中有一个属性DateTimeStart,日期为此格式16/08/2012 08:14:40,我希望使用EF查询,并仅查找日期为16/08/2012的所有实体。使用下面的代码我收到了这个错误 The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are

我使用EF 4,在我的实体中有一个属性
DateTimeStart
,日期为此格式
16/08/2012 08:14:40
,我希望使用EF查询,并仅查找日期为16/08/2012的所有实体
。使用下面的代码我收到了这个错误

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

 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 });
DateTime dateTimeNow=DateTime.UtcNow;
DateTime dateNow=dateTimeNow.Date;
返回db.EventCustoms.Where(x=>x.DataTimeStart.datenew{y.EventId,y.EventTitle,y.DataTimeStart});
DateTime dateTimeNow=DateTime.UtcNow;
DateTime DateTomory=dateTimeNow.Date.AddDays(1);
返回db.EventCustoms.Where(x=>x.DataTimeStartnew{y.EventId,y.EventTitle,y.DataTimeStart});
[编辑]@GibboK,请详细说明一下:

实体框架无法在数据库端转换DateTime对象的属性

你的选择是:

(1) (如上所述)重新考虑您的查询,并尝试一种不需要在数据库端为表中的每一行调用函数的解决方案。。。。这对查询性能也有好处

(2) 或者,如果这是不可能的,您可以使用该类,该类公开可由EF转换为底层数据源的适当本机函数的方法(例如)

e、 g

返回db.EventCustoms
其中(x=>EntityFunctions.TruncateTime(x.DataTimeStart)在EF 6中:

using System.Data.Entity;
...
db.EventCustoms.Where(x => 
 DbFunctions.TruncateTime(x.DataTimeStart) <= DbFunctions.TruncateTime(dateNow))
使用System.Data.Entity;
...
db.EventCustoms.Where(x=>

DbFunctions.TruncateTime(x.DataTimeStart)DataTimeStart的数据类型是什么?您好,Amiram Korach,很遗憾,它不工作,请您看一看并告诉我好吗?非常感谢您的时间或我的错误。它应该是“>=”。如果第一个值小于第二个值,此方法将为您提供正数。今天实现此答案时,我发现
System.Data.Entity.Core.Objects.EntityFunctions
已被弃用,应替换为
System.Data.Entity.DbFunctions
。相同的TruncateTime()方法存在于新的命名空间/类中,因此只需将
EntityFunctions
更改为
DbFunctions
并添加正确的using语句,对于该答案的未来用户应该会很好。但请注意,这将在哪个SQL语句中进行转换。它类似于
((convert(datetime2,convert)(varchar(255),[Filter1])。[DateTimeStart],102),102)
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 }); 
return db.EventCustoms
    .Where(x => EntityFunctions.TruncateTime(x.DataTimeStart) <= dateNow)
using System.Data.Entity;
...
db.EventCustoms.Where(x => 
 DbFunctions.TruncateTime(x.DataTimeStart) <= DbFunctions.TruncateTime(dateNow))