Monday, February 27, 2012

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

Apparently, Entity Framework doesn't allow the use of the Date member of DateTime inside an Entity Framework LINQ Query. The following query fails because of the use of .Date

var salesResult = (from sale in T_Sales
            where ((DateTime)sale.Sale_Date).Date == DateTime.Today
            select sale).ToList();


The above code will raise 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.





I found out form this link that there are EntityFunctions available. So the above code can be replaced with the code below to work:


var salesResult = (from sale in T_Sales
            where EntityFunctions.TruncateTime((DateTime)sale.Sale_Date) == DateTime.Today
            select sale).ToList();




However checking the details of this EntityFunctions on the MSDN site I found out that it is only available to Framework 4.0 and 4.5.

No comments:

Post a Comment