简体   繁体   中英

Compare date part in where clause

When tried to condition the where statement to only compare the date part of the datetime field using this code,

var list =  products.Where(x => ((DateTime)x.PurchaseDate).Date >= dt_SelectDate.Value.Date);

I used the (DateTime) conversion because EF don't recognize the .GetValueOrDefault() , and the PurchaseDate field is nullable.

the error I got is

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

I think you need to the the EntityFunctions.TruncateTime method to compare (just) dates.

Here's the MSDN description: http://msdn.microsoft.com/en-us/library/dd395596.aspx

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