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

时间:2017-04-27 05:57:43

标签: 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,也许这必须以另一种方式完成?

1 个答案:

答案 0 :(得分:3)

  

我在调用Expression.Call时遇到问题,因为op_GreaterThanOrEqual是一个带有2个参数的静态方法

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);
  

也许这必须以另一种方式完成?

每个C#比较运算符都有与GreaterThan等类似的Expression方法,因此您不必费心获取运算符方法信息等,只需使用:

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);