代码之家  ›  专栏  ›  技术社区  ›  Davy Landman

如何在不调用的情况下合并两个C lambda表达式?

  •  12
  • Davy Landman  · 技术社区  · 15 年前

    我想合并以下表达式:

    // example class
    class Order
    {
        List<OrderLine> Lines       
    }
    class OrderLine { }
    
    Expression<Func<Order, List<OrderLine>>> selectOrderLines = o => o.Lines;
    Expression<Func<List<OrderLine>, Boolean>> validateOrderLines = lines => lines.Count > 0;
    
    // now combine those to
    Expression<Func<Order, Boolean>> validateOrder;
    

    我让它在selectorderlines上使用invoke并将结果提供给validateorderlines,但是因为我在实体框架中使用这些表达式,所以我必须实际创建一个干净的表达式,它应该表示:

    Expression<Func<Order, Boolean>> validateOrder = o => o.Lines.Count > 0;
    

    我该怎么做?

    2 回复  |  直到 9 年前
        1
  •  21
  •   Ben M    15 年前

    最优雅的方式是使用 Expression Visitor . 尤其是这个 MSDN Blog Entry 描述如何使用它在不调用的情况下组合谓词(使用布尔值和或)。

    编辑 意识到布尔组合不是您想要的,我编写了一个ExpressionVisitor的示例用法,它可以解决您的特定问题:

    public class ParameterToMemberExpressionRebinder : ExpressionVisitor
    {
        ParameterExpression _paramExpr;
        MemberExpression _memberExpr;
    
        ParameterToMemberExpressionRebinder(ParameterExpression paramExpr, MemberExpression memberExpr) 
        {
            _paramExpr = paramExpr;
            _memberExpr = memberExpr;
        }
    
        protected override Expression Visit(Expression p)
        {
            return base.Visit(p == _paramExpr ? _memberExpr : p);
        }
    
        public static Expression<Func<T, bool>> CombinePropertySelectorWithPredicate<T, T2>(
            Expression<Func<T, T2>> propertySelector,
            Expression<Func<T2, bool>> propertyPredicate)
        {
            var memberExpression = propertySelector.Body as MemberExpression;
    
            if (memberExpression == null)
            {
                throw new ArgumentException("propertySelector");
            }
    
            var expr = Expression.Lambda<Func<T, bool>>(propertyPredicate.Body, propertySelector.Parameters);
            var rebinder = new ParameterToMemberExpressionRebinder(propertyPredicate.Parameters[0], memberExpression);
            expr = (Expression<Func<T, bool>>)rebinder.Visit(expr);
    
            return expr;
        }
    
        class OrderLine
        {
        }
    
        class Order
        {
            public List<OrderLine> Lines;
        }
    
        static void test()
        {
            Expression<Func<Order, List<OrderLine>>> selectOrderLines = o => o.Lines;
            Expression<Func<List<OrderLine>, Boolean>> validateOrderLines = lines => lines.Count > 0;
            var validateOrder = ParameterToMemberExpressionRebinder.CombinePropertySelectorWithPredicate(selectOrderLines, validateOrderLines);
    
            // validateOrder: {o => (o.Lines.Count > 0)}
        }
    }
    
        2
  •  3
  •   Peter Starbek    13 年前

    此扩展工程:

    public static class Utility
        {
            public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
            {
                // build parameter map (from parameters of second to parameters of first)
                var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
    
                // replace parameters in the second lambda expression with parameters from the first
                var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
    
                // apply composition of lambda expression bodies to parameters from the first expression 
                return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
            }
    
            public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
            {
                return first.Compose(second, Expression.And);
            }
    
            public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
            {
                return first.Compose(second, Expression.Or);
            }
        }
    

    样品使用:

    Expression<Func<Product, bool>> filter1 = p => a.ProductId == 1;
    Expression<Func<Product, bool>> filter2 = p => a.Text.StartWith("test");
    Expression<Func<Product, bool>> filterCombined = filter1.And(filter2);