将表达式转换为Expression< Func< T,bool>

将表达式转换为Expression< Func< T,bool>

问题描述:

如果在 T 上创建了Expression的实例,是否可以将 Expression 转换为 Expression< Func< T,bool>> ?

Is that possible to convert Expression to Expression<Func<T, bool>> if instance of Expression was created on T ?

最后,我具有列表 List< Expression> ,并且需要在 Expression< Func< T,bool>> 上生成,其中 List<; Expression> AND 聚合.

At the end I have list List<Expression> and need to produce on Expression<Func<T, bool>> where each expression of List<Expression> is agregated with AND.

是;只需调用 Expression.Lambda< Func< T,bool>>(...,参数),其中 ... 是一个由您要表达的表达式组成的表达式结合起来.

Yes; just call Expression.Lambda<Func<T, bool>>(..., parameter), where ... is an expression composed of the expressions you want to combine.

您可能想要 list.Aggregate(Expressions.AndAlso).

如果您的表达式并不都共享相同的 ParameterExpression ,则需要对其进行重写.(使用 ExpressionVisitor )

If your expressions don't all share the same ParameterExpression, you'll need to rewrite them to do so. (use ExpressionVisitor)