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

后端 未结 10 2019
再見小時候
再見小時候 2020-11-28 02:47

Using this code in Entity Framework I receive the following error. I need to get all the rows for a specific date, DateTimeStart is of type Dat

相关标签:
10条回答
  • 2020-11-28 03:29

    Always use EntityFunctions.TruncateTime() for both x.DateTimeStart and currentDate. such as :

    var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference).Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == EntityFunctions.TruncateTime(currentDate));
    
    0 讨论(0)
  • 2020-11-28 03:29

    Another solution could be:

    var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference).AsEnumerable()
       .Where(x => x.DateTimeStart.Date == currentDate.Date).AsQueryable();
    
    0 讨论(0)
  • 2020-11-28 03:31

    DateTime.Date cannot be converted to SQL. Use EntityFunctions.TruncateTime method to get date part.

    var eventsCustom = eventCustomRepository
    .FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
    .Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date);
    

    UPDATE: As @shankbond mentioned in comments, in Entity Framework 6 EntityFunctions is obsolete, and you should use DbFunctions class, which is shipped with Entity Framework.

    0 讨论(0)
  • 2020-11-28 03:36

    EntityFunctions is obsolete. Consider using DbFunctions instead.

    var eventsCustom = eventCustomRepository.FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference)
       .Where(x => DbFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date);
    
    0 讨论(0)
提交回复
热议问题