如何执行在EF查询日期比较?

编程入门 行业动态 更新时间:2024-10-25 09:28:35
本文介绍了如何执行在EF查询日期比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请帮忙。我想弄清楚如何使用日期或日期时间在LINQ查询比较。

例如:如果我想对那些所有员工姓名谁今天之前开始,我会做的SQL是这样的:

SELECT EmployeeNameColumn从EmployeeTable中WHERE StartDateColumn.Date< = GETDATE()//今天

但对于LINQ?

的DateTime startDT = //今天VAR EmployeeName =从E在db.employee其中,e.StartDateColumn< = startDT

以上的WHERE不起作用:

  

异常详细信息:System.NotSupportedException:指定类型的成员'日期'不是在LINQ支持实体。只有初始化,实体成员和实体导航属性都支持。

解决方案

这应该工作。你肯定没有触发异常的查询的另一部分?我有如下形式的查询的几个实例

VAR的查询=从E在db.MyTable            其中,e.AsOfDate< = DateTime.Now.Date            选择e;

在我的code。

Please help. I am trying to figure out how to use DATE or DATETIME for comparison in a linq query.

Example: If I wanted all Employee names for those who started before today, I would do something like this in SQL:

SELECT EmployeeNameColumn FROM EmployeeTable WHERE StartDateColumn.Date <= GETDATE() //Today

But what about linq?

DateTime startDT = //Today var EmployeeName = from e in db.employee where e.StartDateColumn <= startDT

The above WHERE doesn't work:

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

解决方案

That should work. Are you sure that there isn't another part of the query that triggered the exception? I have several instances of queries of the form

var query = from e in db.MyTable where e.AsOfDate <= DateTime.Now.Date select e;

in my code.

更多推荐

如何执行在EF查询日期比较?

本文发布于:2023-11-03 12:28:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!