如何在 EF 查询中执行日期比较? [英] How do I perform Date Comparison in EF query?

查看:74
本文介绍了如何在 EF 查询中执行日期比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙.我想弄清楚如何在 linq 查询中使用 DATE 或 DATETIME 进行比较.

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

示例:如果我想要所有在今天之前开始的员工的名字,我会在 SQL 中做这样的事情:

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

但是 linq 呢?

DateTime startDT = //Today

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

以上 WHERE 不起作用:

The above WHERE doesn't work:

异常详细信息:System.NotSupportedException:LINQ to Entities 不支持指定的类型成员Date".仅支持初始值设定项、实体成员和实体导航属性.

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;

在我的代码中.

这篇关于如何在 EF 查询中执行日期比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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