简体   繁体   中英

The specified type member 'Date' is not supported in LINQ to Entities Exception - When comparing two DateTime values in LINQ where condition

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

when trying to compare two Datetime values in a LINQ where condition.

I have used DbFunctions.TruncateTime as well, but still Im getting the same exception.

var now = System.DateTime.Now;    
documents.Where(d => DbFunctions.TruncateTime(d.ExpirationDate.Date) < DbFunctions.TruncateTime(now));

How about ?

var now = System.DateTime.Now;    
documents.Where(d => d.ExpirationDate < now)

or

var now = System.DateTime.Now;    
documents.Where(d => d.ExpirationDate.Date < now.Date)

The actual issue was with .Date portion. So instead of using

var now = System.DateTime.Now;    
documents.Where(d => DbFunctions.TruncateTime(d.ExpirationDate.Date) < DbFunctions.TruncateTime(now));

I'm using

var now = System.DateTime.Now;    
documents.Where(d => DbFunctions.TruncateTime(d.ExpirationDate) < DbFunctions.TruncateTime(now));

Furthermore, following also works without any issues.

var now = System.DateTime.Now;    
documents.Where(d => d.ExpirationDate < now)

Credit should go to this resource - The specified type member 'Date' is not supported in LINQ to Entities Exception - Entity Framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM