I come across the following error while comparing Date in Linq lambda in the .NET C# project. The exact error is

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

My code below

 _db.Products.Any(i => i.VariationId == variantid && i.LastUpdate.Date > DateTime.Now.Date);

I was using the visual studio 2022 community edition. When I investigate the issue, it seems like DateTime.Date cannot be converted to SQL

Solution

The solution is to use

DbFunctions.TruncateTime(DateTime)

The modified code will be like below

_db.Products.Any(i => i.VariationId == variantid && DbFunctions.TruncateTime(i.LastUpdate) < DateTime.Now);

Categorized in: