使用 DateTime 过滤器构建 LINQ to Entities 表达式树

Building LINQ to Entities Expression tree with DateTime filter

我正在尝试完成 Todd Sprang 提供的动态查询的实现,请参阅此 link。 目前我正在尝试启用按 DateTime 类型的过滤,并且我正在努力使用 "greater than" 和 "less than" 运算符制作 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);