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

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);

About the author

Naveed Ul-Haq

I am a UK-based Technical Architect, Founder & Technology Evangelist. I'm Optimizely MVP & Optimizely SME on Content Cloud and Commerce Cloud. I love working on .NET-based CMS, eCommerce solutions, .NET Core, DevOps, and Cloud computing. I'm also a Certified Optimizely Content Cloud Developer, Certified Optimizely Commerce Cloud developer, Optimizely B2B Commerce developer and Microsoft Certified Professional in Azure application development. I spend my free time with my family and reading books. You can contact me at hello@naveedulhaq.com

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *