Combining two expressions (Expression>)

前端 未结 7 1556
无人共我
无人共我 2020-11-22 01:09

I have two expressions of type Expression> and I want to take to OR, AND or NOT of these and get a new expression of the same type

7条回答
  •  臣服心动
    2020-11-22 01:40

    If you provider does not support Invoke and you need to combine two expression, you can use an ExpressionVisitor to replace the parameter in the second expression by the parameter in the first expression.

    class ParameterUpdateVisitor : ExpressionVisitor
    {
        private ParameterExpression _oldParameter;
        private ParameterExpression _newParameter;
    
        public ParameterUpdateVisitor(ParameterExpression oldParameter, ParameterExpression newParameter)
        {
            _oldParameter = oldParameter;
            _newParameter = newParameter;
        }
    
        protected override Expression VisitParameter(ParameterExpression node)
        {
            if (object.ReferenceEquals(node, _oldParameter))
                return _newParameter;
    
            return base.VisitParameter(node);
        }
    }
    
    static Expression> UpdateParameter(
        Expression> expr,
        ParameterExpression newParameter)
    {
        var visitor = new ParameterUpdateVisitor(expr.Parameters[0], newParameter);
        var body = visitor.Visit(expr.Body);
    
        return Expression.Lambda>(body, newParameter);
    }
    
    [TestMethod]
    public void ExpressionText()
    {
        string text = "test";
    
        Expression> expr1 = p => p.Item1.Contains(text);
        Expression> expr2 = q => q.Item2.Contains(text);
        Expression> expr3 = UpdateParameter(expr2, expr1.Parameters[0]);
    
        var expr4 = Expression.Lambda>(
            Expression.OrElse(expr1.Body, expr3.Body), expr1.Parameters[0]);
    
        var func = expr4.Compile();
    
        Assert.IsTrue(func(new Coco { Item1 = "caca", Item2 = "test pipi" }));
    }
    

提交回复
热议问题