c# - 在 DateTime 上动态过滤 ObjectQuery<T>

标签 c# entity-framework

我有以下方法用于使用 EF 按日期过滤 ObjectQuery:

public List<T> FilterObjectSetByDate<T>(ObjectQuery<T> inputQuery, string dateColumn) where T : class
{
    ObjectQuery<T> filteredQuery = inputQuery.Where("(dateadd(dd,0, datediff(dd,0, it" + dateColumn + ")) = @p0)");
    ObjectParameter objParam = new ObjectParameter("p0", DateTime.Now);
    filteredQuery.Parameters.Add(objParam);

    return filteredQuery.ToList();
}      

这会导致以下错误:

'dateadd' cannot be resolved into a valid type or function.

有没有什么方法可以使用 Entity Framework 返回给定日期的记录列表(忽略 DateTime 的时间部分)?

最佳答案

好的,今天早上想通了:

Where("SqlServer.datediff('DAY'," + dateColumn + ", @p{1}) = 0")

关于c# - 在 DateTime 上动态过滤 ObjectQuery<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12259857/

相关文章:

c# - 如何使用 Include() 只获取第一个元素?

entity-framework - Entity Framework - SSDL 和 CSDL 中的不同多重性

ASP.Net 分层应用程序 - 在层之间共享实体数据模型

c# - 使用linq从xml文件中查找

c# - 如何使用 Entity Framework 执行插入查询

c# - TCP/IP 套接字端口监听

.net - 如何使用 Entity Framework 将数据添加到 SQL Server Express 数据库

c# - 对 IQueryable 对象的多次求值是否会多次访问数据库?

c# - Web 配置中的连接字符串

c# - 使用 C# 从 URL 反序列化 XML