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

后端 未结 10 2122
再見小時候
再見小時候 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:18

    Just use simple properties.

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

    If future dates are not possible in your app, then >= x.DateTimeStart >= currentDateTime.Date is sufficient.

    if you have more complex date comparisons, then check Canonical functions and if you have EF6+ DB functions

    More Generally - For people searching for issues Supported Linq methods in EF can explain similar issues with linq statements that work on Memory base Lists but not in EF.

提交回复
热议问题