DbFunctions.TruncateTime() still producing "type member 'Date' is not supported" error

c# entity-framework-6 linq

Question

The LINQ query below is really giving me a hard time. I've read a lot about this problem, and practically everything I've read mentions using DbFunctions. TruncateTime(), however it doesn't appear to be working for me anymore. I'm hoping someone will look at this and tell me where I'm making a mistake. I simply want to compare the date component of the datetime, but I keep getting the error Exception: The member of the type 'Date' that was supplied is not supported by LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported..

if (!db.MaintenanceWindows.Any(x => x.Id == Id & 
DbFunctions.TruncateTime(x.BlockedDay) == DateTime.UtcNow.Date & 
x.IsDeleted.Value == false))
{
...
}

TIA

1
1
7/7/2017 2:20:32 PM

Popular Answer

It is not criticising theDbFunctions.TruncateTime . It is criticising theDateTime.UtcNow.Date

Option A: zzzz-13 zzzz

if (!db.MaintenanceWindows.Any(x => x.Id == Id && 
    DbFunctions.TruncateTime(x.BlockedDay) == DbFunctions.TruncateTime(DateTime.UtcNow) && 
    x.IsDeleted.Value == false))

B. Suggestion

var utcNow = DateTime.UtcNow.Date;
if (!db.MaintenanceWindows.Any(x => x.Id == Id && 
    DbFunctions.TruncateTime(x.BlockedDay) == utcNow && 
    x.IsDeleted.Value == false))

Additionally, you'll need the "logical AND" (&& ) in the sentence.

0
10/17/2019 5:14:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow