"The specified type member 'Date' is not supported in LINQ "

c# entity-framework lambda linq

Question

_dbEntities.EmployeeAttendances.Where(x => x.DailyDate.Date.Equals(DateTime.Now.Date)).ToList();

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

How can I use a Linq query to get employees' data depending on the current date?

1
4
2/7/2015 11:34:32 AM

Accepted Answer

EntityFramework unable to convertDateTime.Date to SQL. Thus, it is unable to provide the desired SQL. Instead, you might useEntityFunctions.TruncateTime() or DbFunctions.TruncateTime() (Given thatEF technique if you want to acquire the latestDate only in part

 _dbEntities.EmployeeAttendances
            .Where(x => EntityFunctions.TruncateTime(x.DailyDate) == DateTime.Now.Date)
            .ToList();

Additional details

EntityFunctions They are called canonical procedures techniques. These are a group of features that all Entity Framework providers implement. For the provider, these canonical functions will be converted into the matching data source capabilities. Because they keep the query portable, canonical functions are the preferred method of accessing functionality outside the core language.

Canonical functions here and Date and Time Canonical Functions here are both available.

Update:

since EF6EntityFunctions has been out of date forSystem.Data.Entity.DbFunctions .

16
1/22/2018 8:44:50 AM

Popular Answer

Use EntityFunctions sparingly in EF 6. TruncateTime is a DbFunctions class function.

DbFunctions.TruncateTime(x.DailyDate)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow