有没有办法使用||或&&表达式之间的操作符
本文关键字:表达式 之间 操作符 有没有 | 更新日期: 2023-09-27 18:18:06
我正在尝试重构一个复杂的Linq查询。我想知道我们是否有办法做这样的事情:假设有两个表达式:
Expression<Func<object,bool>> exp1=....;
Expression<Func<object,bool>> exp2=....;
Expression<Func<object,bool>> exp3=exp1 || exp2;
基本上您需要重新编写其中一个树,以便您可以组合表达式。幸运的是,ExpressionVisitor
帮助了我们:
static void Main()
{
Expression<Func<object, bool>> exp1 = x => ((string)x).StartsWith("a");
Expression<Func<object, bool>> exp2 = y => ((string)y).StartsWith("b");
// the two expressions here are purely for illustration
var exp3 = Combine(exp1, exp2, Expression.OrElse); // i.e. ||
var exp4 = Combine(exp1, exp2, Expression.AndAlso); // i.e. &&
}
static Expression<Func<TValue, TResult>> Combine<TValue, TResult>(
Expression<Func<TValue, TResult>> left,
Expression<Func<TValue, TResult>> right,
Func<Expression, Expression, BinaryExpression> combination)
{
// rewrite the body of "right" using "left"'s parameter in place
// of the original "right"'s parameter
var newRight = new SwapVisitor(right.Parameters[0], left.Parameters[0])
.Visit(right.Body);
// combine via && / || etc and create a new lambda
return Expression.Lambda<Func<TValue, TResult>>(
combination(left.Body, newRight), left.Parameters);
}
class SwapVisitor : ExpressionVisitor {
private readonly Expression from, to;
public SwapVisitor(Expression from, Expression to) {
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node) {
return node == from ? to : base.Visit(node);
}
}
注意,这也允许其他任意的使用,例如:
Expression<Func<object, int>> exp1 = x => x.GetHashCode();
Expression<Func<object, int>> exp2 = y => y.ToString().Length;
var exp3 = Combine(exp1, exp2, Expression.Add);
// which is: x => x.GetHashCode() + x.ToString().Length;
我想不出实用的用法。p
您可以使用Dynamic Linq Query或PredicateBuilder