替换表达式主体中的参数名称
我正在尝试基于规范对象动态构建表达式。
我创建了一个 ExpressionHelper 类,它有一个私有表达式,如下所示:
private Expression<Func<T, bool>> expression;
public ExpressionHelper()
{
expression = (Expression<Func<T, bool>>)(a => true);
}
然后是一些简单的方法,如下所示:
public void And(Expression<Func<T,bool>> exp);
我正在努力处理 And 方法的主体。我基本上想将主体从 exp
中剥离出来,将所有参数替换为 expression
中的参数,然后将其附加到 expression
的末尾体为 和 AndAlso。
我已经这样做了:
var newBody = Expression.And(expression.Body,exp.Body);
expression = expression.Update(newBody, expression.Parameters);
但最终我的表情看起来像这样:
{ a => e.IsActive && e.IsManaged }
有没有更简单的方法来做到这一点?或者我怎样才能撕掉那些 e 并用 a 替换它们?
I'm trying to dynamically build up expressions based on a Specification object.
I've created an ExpressionHelper class that has a private Expression like so:
private Expression<Func<T, bool>> expression;
public ExpressionHelper()
{
expression = (Expression<Func<T, bool>>)(a => true);
}
And then some easy methods as follows:
public void And(Expression<Func<T,bool>> exp);
I'm struggling with the body of the And method. I basically want to rip the body out of exp
, replace all the parameters with those in expression
and then append it to the end of the expression
body as and AndAlso.
I've done this:
var newBody = Expression.And(expression.Body,exp.Body);
expression = expression.Update(newBody, expression.Parameters);
But that ends up with my expression looking like this:
{ a => e.IsActive && e.IsManaged }
Is there a simpler way to do this? Or how can I rip out those e's and replace them with a's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里最简单的方法是
Expression.Invoke
,例如:这对于 LINQ-to-Objects 和 LINQ-to-SQL 效果很好,但 EF 不支持。遗憾的是,对于 EF,您需要使用访问者来重写树。
使用以下代码: 在 c# 中组合两个 lambda 表达式
或者在 .NET 4.0 中,使用
ExpressionVisitor
:The simplest approach here is
Expression.Invoke
, for example:This works fine for LINQ-to-Objects and LINQ-to-SQL, but isn't supported by EF. For EF you'll need to use a visitor to rewrite the tree, sadly.
Using the code from: Combining two lambda expressions in c#
Or in .NET 4.0, using
ExpressionVisitor
: