我想在 BinaryExpression 中组合条件列表。问题是我希望所有条件都在同一级别,这样:
True == True And True == False And True == True And True == True And True == True Or True == False And True == True And True == True And True == True And True == True And真 == 真或真 == 真
但是生成了表达式:
(((((((((((((True == True) And (True == False)) And (True == True)) And (True == True)) And (True == True))) 或者(True == False)) And (True == True)) And (True == True)) And (True == True)) And (True == True)) And (True == True)) Or (True == 真))
var currentExpr = this.MakeExpression(list.First());
foreach (bool obj in list.Skip(1))
{
var nextExpr = this.MakeExpression(obj);
switch (type)
{
case 1: // And
currentExpr = Expression.And(currentExpr, nextExpr);
break;
case 2: // Or
currentExpr = Expression.Or(currentExpr, nextExpr);
break;
}
}
private BinaryExpression MakeExpression(bool value)
{
BinaryExpression expression = Expression.Equal(Expression.Constant(true),Expression.Constant(value));
return expression;
}