具有多个输入选择器的Expression.Lambda

时间:2015-01-20 03:12:49

标签: c# linq lambda

我正在尝试创建自己的自定义操作,我可以在数据库中使用它来查找受值更改影响的行。

我在这里看操作员示例之间的Jon Skeets:LINQ Between Operator但我遇到麻烦,因为我的操作包含多个参数输入

    public static IQueryable<TSource> LeavingRange<TSource, TKey>(this IQueryable<TSource> source,
        Expression<Func<TSource, TKey>> lowKeySelector,
        Expression<Func<TSource, TKey>> highKeySelector,
        Nullable<TKey> oldValue,
        Nullable<TKey> newValue)
            where TKey : struct, IComparable<TKey>

正如您所看到的,我有2个选择器,但是我不太确定如何将这些正确地组合到我的Expression.Lambda调用的参数中。我已经尝试将两个输入表达式中的参数放入lambda作为参数,但我认为我遗漏了一些东西。

Expression.Lambda<Func<TSource, bool>>(isLeavingRange, lowKeySelector.Parameters[0], highKeySelector.Parameters[0]);

执行此操作会出现以下错误:

  

为lambda声明提供的参数数量不正确

构建Lambda时组合输入参数的正确方法是什么?


支持信息

我的完整代码如下,但我认为相关位是两个选择器和Expression.Lambda调用

    public static IQueryable<TSource> LeavingRange<TSource, TKey>(this IQueryable<TSource> source,
        Expression<Func<TSource, TKey>> lowKeySelector,
        Expression<Func<TSource, TKey>> highKeySelector,
        Nullable<TKey> oldValue,
        Nullable<TKey> newValue)
            where TKey : struct, IComparable<TKey>
    {
        Expression lowKey = Expression.Invoke(lowKeySelector, lowKeySelector.Parameters.ToArray());
        Expression highKey = Expression.Invoke(highKeySelector, highKeySelector.Parameters.ToArray());

        //is oldValue null which means it cant possibly be leaving
        var oldValueIsNotNull = Expression.NotEqual(Expression.Constant(oldValue, typeof(Nullable<TKey>)), Expression.Constant(null, typeof(Nullable<TKey>)));
        var newValueIsNull = Expression.Equal(Expression.Constant(newValue, typeof(Nullable<TKey>)), Expression.Constant(null, typeof(Nullable<TKey>)));
        var newValueIsNotNull = Expression.Not(newValueIsNull);

        var oldValueIsBetweenRange = Between(Expression.Convert(Expression.Constant(oldValue), typeof(TKey)), lowKey, highKey);
        var newValueIsNotBetweenRange = Expression.Not(Between(Expression.Convert(Expression.Constant(newValue), typeof(TKey)), lowKey, highKey));

        //IE leaving because its going from in the range to null
        var newValueIsNullAndOldValueIsBetweenRange = Expression.AndAlso(newValueIsNull, oldValueIsBetweenRange);

        var oldValueIsInRangeAndNewValueIsNot = Expression.AndAlso(newValueIsNotNull, Expression.AndAlso(oldValueIsBetweenRange, newValueIsNotBetweenRange));
        var isLeavingRange = Expression.AndAlso(oldValueIsNotNull, Expression.Or(newValueIsNullAndOldValueIsBetweenRange, oldValueIsInRangeAndNewValueIsNot));

        var leavingRange = Expression.Lambda<Func<TSource, bool>>(isLeavingRange, lowKeySelector.Parameters[0], highKeySelector.Parameters[0]);

        return source.Where(leavingRange);
    }

1 个答案:

答案 0 :(得分:2)

传递给Where()的委托只将集合中的每个元素作为参数, 所以你需要让两个表达式调用lowKeySelectorhighKeySelector将相同的元素(ParameterExpression的相同实例)作为参数 并且还需要构建lambda表达式以将其用作参数。

public static IQueryable<TSource> LeavingRange<TSource, TKey>(this IQueryable<TSource> source,
     Expression<Func<TSource, TKey>> lowKeySelector,
     Expression<Func<TSource, TKey>> highKeySelector,
     Nullable<TKey> oldValue,
     Nullable<TKey> newValue)
         where TKey : struct, IComparable<TKey>
{
    ParameterExpression paramOfWhereDelg = Expression.Parameter(typeof(TSource), "p");

    Expression lowKey = Expression.Invoke(lowKeySelector, paramOfWhereDelg);
    Expression highKey = Expression.Invoke(highKeySelector, paramOfWhereDelg);

    // Build your expression tree
    //  ...

    var leavingRange = Expression.Lambda<Func<TSource, bool>>(isLeavingRange, paramOfWhereDelg);

    return source.Where(leavingRange);
}

(或者您可以使用lowKeySelector.Parameters代替paramOfWhereDelg, 但我相信创建另一个ParameterExpression会使其更容易理解。)