"指定类型的成员'日期'不是在LINQ"的支持; [英] "The specified type member 'Date' is not supported in LINQ "
问题描述
_dbEntities.EmployeeAttendances.Where(X => x.DailyDate.Date.Equals(DateTime.Now.Date))。了ToList();
指定类型的成员'日期'不是在LINQ支持
实体,只有初始化,实体成员和实体导航
属性都支持。
块引用>
我可以如何做此得到员工的数据基础上,LINQ查询当前的日期?
解决方案
的EntityFramework
不能DateTime.Date
转换为SQL。因此,它不能产生预期的SQL。相反的,你可以使用EntityFunctions.TruncateTime()
方法,如果你想获得日期
部分只:_dbEntities.EmployeeAttendances
。凡(X => EntityFunctions.TruncateTime(x.DailyDate)== DateTime.Now.Date )
.ToList();
附加信息:的
EntityFunctions
方法被调用的的规范函数的。而这些是一组功能,这是由所有的实体框架提供商都支持。这些规范的功能将被转换为提供相应的数据源功能。 。规范功能是语言的核心外部访问功能的首选方法,因为它保留查询便携
您可以找到所有经典功能的 这里 和所有日期和时间规范函数< A HREF =https://msdn.microsoft.com/en-us/library/bb738563(v=vs.110).aspx相对=nofollow> 这里 。
_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?
解决方案
EntityFramework
cannot convertDateTime.Date
to SQL. So, it fails to generate expected SQL. Instead of that you can useEntityFunctions.TruncateTime()
method if you want to getDate
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.
这篇关于"指定类型的成员'日期'不是在LINQ"的支持;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!