Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

_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 do this get employees data on based on current date in linq query?

like image 256
Mike Avatar asked Nov 27 '22 02:11

Mike


2 Answers

EntityFramework cannot convert DateTime.Date to SQL. So, it fails to generate expected SQL. Instead of that you can use EntityFunctions.TruncateTime() or DbFunctions.TruncateTime()(based on EF version) method if you want to get Date part only:

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

Additional info:

EntityFunctions methods are called canonical functions. And these are a set of functions, which are supported by all Entity Framework providers. These canonical functions will be translated to the corresponding data source functionality for the provider. Canonical functions are the preferred way to access functionality outside the core language, because they keep the queries portable.

You can find all canonical functions here and all Date and Time Canonical Functions here.

Update:

As of EF6 EntityFunctions has been deprecated for System.Data.Entity.DbFunctions.

like image 154
Farhad Jabiyev Avatar answered Dec 05 '22 23:12

Farhad Jabiyev


Dont use EntityFunctions in EF 6. TruncateTime is in the DbFunctions class:

DbFunctions.TruncateTime(x.DailyDate)
like image 35
bromose Avatar answered Dec 06 '22 00:12

bromose