LINQ to Entities不支持指定的类型成员"Date".仅初始化程序,实体成员和实体导航属性

Gib*_*boK 124 linq linq-to-entities entity-framework-4

Entity Framework中使用此代码我收到以下错误.我需要获取特定日期的所有行,采用DateTimeStart此格式的DataType类型2013-01-30 12:00:00.000

码:

 var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
                    .Where(x =>  x.DateTimeStart.Date == currentDateTime.Date);
Run Code Online (Sandbox Code Playgroud)

错误:

base {System.SystemException} = {"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)

任何想法如何解决它?

Ser*_*kiy 251

DateTime.Date无法转换为SQL.使用EntityFunctions.TruncateTime方法获取日期部分.

var eventsCustom = eventCustomRepository
.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
.Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date);
Run Code Online (Sandbox Code Playgroud)

更新:正如@shankbond在评论中提到的,实体框架6 EntityFunctions已经过时了,你应该使用DbFunctions随实体框架一起提供的类.

  • EntityFunctions已过时,而是使用DbFunctions.TruncateTime方法 (59认同)
  • LINQ to Entities 不支持指定的类型成员“日期”。仅支持初始值设定项、实体成员和实体导航属性。 (2认同)

WaZ*_*WaZ 74

你现在应该使用 DbFunctions.TruncateTime

var anyCalls = _db.CallLogs.Where(r => DbFunctions.TruncateTime(r.DateTime) == callDateTime.Date).ToList();
Run Code Online (Sandbox Code Playgroud)


Leo*_*lva 14

EntityFunctions已经过时了.考虑DbFunctions改用.

var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
   .Where(x => DbFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date);
Run Code Online (Sandbox Code Playgroud)


jvr*_*nte 13

我想添加一个解决方案,帮助我解决实体框架中的这个问题:

var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
                .Where(x =>  x.DateTimeStart.Year == currentDateTime.Year &&
                             x.DateTimeStart.Month== currentDateTime.Month &&
                             x.DateTimeStart.Day == currentDateTime.Day
    );
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助.


Bab*_*dha 8

始终对x.DateTimeStart和currentDate使用EntityFunctions.TruncateTime().如 :

var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference).Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == EntityFunctions.TruncateTime(currentDate));
Run Code Online (Sandbox Code Playgroud)


phi*_*ady 7

只需使用简单的属性。

var tomorrow = currentDateTime.Date + 1;  
var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
                            .Where(x =>  x.DateTimeStart >= currentDateTime.Date 
                                   and x.DateTimeStart < tomorrow);
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序中不可能有未来的日期,那么>= x.DateTimeStart >= currentDateTime.Date就足够了。

如果您有更复杂的日期比较,请检查Canonical 函数 ,如果您有 EF6+ DB 函数

更一般的 - 对于搜索问题的人,EF 中支持的 Linq 方法 可以解释与适用于内存库列表但不适用于 EF 的 linq 语句的类似问题。


小智 5

简化:

DateTime time = System.DateTime.Now;
ModelName m = context.TableName.Where(x=> DbFunctions.TruncateTime(x.Date) == time.Date)).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)