Tuesday 5 June 2012

Date Comparision in Entity Framework

When we are writing linq expression in Entity framework as follows
context.GetQuery<TaxLocExtract>().Where(
tle => tle.EffectiveDate.Date == effectiveDate.Date).ToList();
then we will get the following exception
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
the solution to resolve is System.Data.Objects.EntityFunctions.TruncateTime() is used. the sample query as follows.
context.GetQuery<TaxLocExtract>().Where(
tle => System.Data.Objects.EntityFunctions.TruncateTime(tle.EffectiveDate) == System.Data.Objects.EntityFunctions.TruncateTime(effectiveDate)).ToList();
I hope this solution helps.