“LINQ”中不支持指定的类型成员“Date” [英] "The specified type member 'Date' is not supported in LINQ "

查看:73
本文介绍了“LINQ”中不支持指定的类型成员“Date”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




LINQ中不支持指定的类型成员'Date'
实体只支持初始化器,实体成员和实体导航
属性。

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

这样做可以根据linq查询中的当前日期获取员工数据?

How can i do this get employees data on based on current date in linq query?

推荐答案

EntityFramework 不能将$ code> DateTime.Date 转换为SQL。因此,它无法生成预期的SQL。如果你想获得日期,那么你可以使用 EntityFunctions.TruncateTime()方法:

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

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

附加信息:

Additional info:

EntityFunctions 方法称为 规范功能 。这些功能是由所有实体框架提供商支持的。这些规范函数将被转换为提供者的相应数据源功能。标准功能是访问核心语言之外的功能的首选方式,因为它们可以保持查询的可移植性。

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.

您可以找到所有规范函数 这里 和所有日期和时间规范函数< a href =https://msdn.microsoft.com/en-us/library/bb738563(v=vs.110).aspx =noreferrer> 这里

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

这篇关于“LINQ”中不支持指定的类型成员“Date”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆