c# - 使用日期时间筛选器构建 LINQ to Entities 表达式树

标签 c# lambda linq-to-entities

我正在尝试完成 Todd Sprang 提供的动态查询的实现,请参阅此 link 。 目前我正在尝试按 DateTime 类型启用过滤,并且我正在努力使用“大于”和“小于”运算符来创建 lambda 表达式,因此我的 SQL 将显示为“WHERE someDate<='2017-04-27 00: 00:00' AND someDate<='2017-04-27 23:59:59'。

我设法提取了一个运算符方法:

private static readonly MethodInfo DateTimeGreaterThanOrEqualMethod = typeof(DateTime).GetMethod("op_GreaterThanOrEqual",
                                BindingFlags.Static | BindingFlags.Public);
private static readonly MethodInfo DateTimeLessThanOrEqualMethod = typeof(DateTime).GetMethod("op_LessThanOrEqual",
                        BindingFlags.Static | BindingFlags.Public);

这是我的 DateTime lambda 过滤器方法:

    private static Expression<Func<TDbType, bool>> ApplyDateTimeCriterion<TDbType,
            TSearchCriteria>(TSearchCriteria searchCriteria, PropertyInfo searchCriterionPropertyInfo,
            Type dbType, MemberInfo dbFieldMemberInfo, Expression<Func<TDbType, bool>> predicate)
{
    var searchDateTime = searchCriterionPropertyInfo.GetValue(searchCriteria) as DateTime?;
    if (searchDateTime == null)
    {
        return predicate;
    }
    var valueDateMin = ((DateTime)searchDateTime).Date;
    var valueDateMax = new DateTime(valueDateMin.Year, valueDateMin.Month, valueDateMin.Day, 23, 59, 59);

    var dbTypeParameter = Expression.Parameter(dbType, @"x");
    var dbFieldMember = Expression.MakeMemberAccess(dbTypeParameter, dbFieldMemberInfo);
    var criterionConstantMin = new Expression[] { Expression.Constant(valueDateMin) };
    var criterionConstantMax = new Expression[] { Expression.Constant(valueDateMax) };
    //having problem down here, when trying to call static method which needs two parameters
    var greaterThanCall = Expression.Call(dbFieldMember, DateTimeGreaterThanOrEqualMethod, criterionConstantMin);
    var lambdaGreaterThan = Expression.Lambda(greaterThanCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;

    var lessThanCall = Expression.Call(dbFieldMember, DateTimeLessThanOrEqualMethod, criterionConstantMax);
    var lambdaLessThan = Expression.Lambda(greaterThanCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;

    return predicate.And(lambdaGreaterThan).And(lambdaLessThan);
}

我在调用 Expression.Call 时遇到问题,因为 op_GreaterThanOrEqual 是带有 2 个参数的静态方法。也许我不应该调用 op_GreaterThanOrEqual,也许这必须以另一种方式完成?

最佳答案

I am having problem on calling Expression.Call because op_GreaterThanOrEqual is a static method with 2 parameters

Expression.Call 有多个重载,您需要为静态方法使用正确的重载(当前您正在使用带有 instance 参数的重载,因此为实例方法) .

也不要创建不必要的Expression[](接收多个表达式的方法通常使用params Expression[],因此会在需要时为您创建) :

var criterionConstantMin = Expression.Constant(valueDateMin);
var criterionConstantMax = Expression.Constant(valueDateMax);

这是有问题的电话

var greaterThanCall = Expression.Call(DateTimeGreaterThanOrEqualMethod, dbFieldMember, criterionConstantMin);
var lessThanCall = Expression.Call(DateTimeLessThanOrEqualMethod, dbFieldMember, criterionConstantMax);

maybe this has to be done in another way?

有特定的Expression方法,例如GreaterThan等对应于每个 C# 比较运算符,因此您不必费心获取运算符方法信息等,只需使用:

var greaterThanCall = Expression.GreaterThanOrEqual(dbFieldMember, criterionConstantMin);
var lessThanCall = Expression.LessThanOrEqual(dbFieldMember, criterionConstantMax);

更新:为了支持 DateTime? 成员,请使用以下内容:

var criterionConstantMin = Expression.Constant(valueDateMin, dbFieldMember.Type);
var criterionConstantMax = Expression.Constant(valueDateMax, dbFieldMember.Type);

然后

var greaterThanCall = Expression.GreaterThanOrEqual(dbFieldMember, criterionConstantMin);
var lessThanCall = Expression.LessThanOrEqual(dbFieldMember, criterionConstantMax);

关于c# - 使用日期时间筛选器构建 LINQ to Entities 表达式树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43649673/

相关文章:

c# - 使用 linq/lambda 选择最早的日期

C# Lambda 表达式通过逗号分隔的 id 提取所有名称

c# - 结构重叠或错误对齐

c# - WPF: MemoryStream 占用大量内存

java - 使用 lambda 在 LinkedList 中查找元素

c# - 使用 Entity Framework 获取特定类型的实体返回错误 - LINQ to Entities 无法识别方法 'System.Type GetType()'

c# - 具有内部联接、多个分组依据和最小最大值的 Linq 查询

c# - User.Identity.Name - 哪个程序集引用将其引入您的项目?

c# - 如何在 ASP.NET 中重写我的 URL?

FilenameFilter 的 java 8 lambda 表达式