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

c# entity-framework-6 linq

Question

I'm really having trouble with the LINQ query below. I did a lot of research on this error and almost everything i have found references the use of DbFunctions.TruncateTime(), however this still does not seem to be working for me. I'm hoping someone can take a look at this and let me know where i am going wrong. All i am trying to do is compare only the date portion of the datetime but i continue to get the Exception: The specified type member 'Date' is not supported in 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's not complaining about the DbFunctions.TruncateTime. It's complaining about the DateTime.UtcNow.Date

Suggestion A: (not recommended)

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

Suggestion B:

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

Also, you are going to want the "logical AND" (&&) in the expression.

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