如何组成表达式:选择器+谓词?

问题描述 投票:0回答:2

假设我们有两个班级

public class EntityA
{
    public EntityB EntityB { get; set; }
}

public class EntityB
{
    public string Name { get; set; }
    public bool IsDeleted { get; set; }
}

以及选择器和谓词的两个表达式

Expression<Func<EntityA, EntityB>> selector = c => c.EntityB;
Expression<Func<EntityB, bool>> predicate = c => c.IsDeleted && c.Name == "AAA";

我需要编写一个返回组合表达式的方法,例如a

Expression<Func<TSource, bool>> Compose<TPropType>(Expression<Func<TSource, TPropType>> selector, Expression<Func<TPropType, bool>> predicator)
{
    // Expression API ???
}

在我的示例中结果应该是

Expression<Func<EntityA, bool>> exp = Compose(selector, predicate);

相当于什么

Expression<Func<EntityA, bool>> exp = c => c.EntityB.IsDeleted && c.EntityB.Name == "AAA";
linq entity-framework lambda expression-trees
2个回答
4
投票

调用这些 lambda 表达式肯定是您不想做的事情。你应该做的是重写表达式。您只需要一种将值绑定到 lambda 表达式的方法,就像调用它们一样。为此,请重写表达式的主体,将参数替换为要绑定的值。您可以使用此

SubstitutionVisitor
来帮助做到这一点:

public class SubstitutionVisitor : ExpressionVisitor
{
    public Expression OldExpr { get; set; }
    public Expression NewExpr { get; set; }

    public override Expression Visit(Expression node)
    {
        return (node == OldExpr) ? NewExpr : base.Visit(node);
    }
}

例如,给出这些表达式:

Expression<Func<EntityA, EntityB>> selector =
    entityA => entityA.EntityB;
Expression<Func<EntityB, bool>> predicate =
    entityB => entityB.IsDeleted && entityB.Name == "AAA";

目标是有效地重写它,使其变成这样:

Expression<Func<EntityA, bool>> composed =
    entity => entity.EntityB.IsDeleted && entity.EntityB.Name == "AAA";
static Expression<Func<TSource, bool>> Compose<TSource, TProp>(
    Expression<Func<TSource, TProp>> selector,
    Expression<Func<TProp, bool>> predicate)
{
    var parameter = Expression.Parameter(typeof(TSource), "entity");
    var property = new SubstitutionVisitor
    {
        OldExpr = selector.Parameters.Single(),
        NewExpr = parameter,
    }.Visit(selector.Body);
    var body = new SubstitutionVisitor
    {
        OldExpr = predicate.Parameters.Single(),
        NewExpr = property,
    }.Visit(predicate.Body);
    return Expression.Lambda<Func<TSource, bool>>(body, parameter);
}

为了了解这里发生了什么,这里有逐行解释:

  1. 为我们正在创建的新 lambda 创建一个新参数。

    entity => ...
    
  2. 给定选择器,将原始参数

    entityA
    的所有实例替换为 lambda 主体中的新参数
    entity
    以获得属性。

    entityA => entityA.EntityB
    // becomes
    entity.EntityB
    
  3. 给定谓词,将原始参数

    entityB
    的所有实例替换为之前从 lambda 主体获得的属性
    entity.EntityB
    ,以获得新 lambda 的主体。

    entityB => entityB.IsDeleted && entityB.Name == "AAA"
    // becomes
    entity.EntityB.IsDeleted && entity.EntityB.Name == "AAA"
    
  4. 将它们全部放入新的 lambda 中。

    entity => entity.EntityB.IsDeleted && entity.EntityB.Name == "AAA"
    

0
投票

您可以尝试以下方法:

static Expression<Func<TSource, bool>> Compose<TSource, TPropType>(
    Expression<Func<TSource, TPropType>> selector,
    Expression<Func<TPropType, bool>> predicator)
{
    ParameterExpression param = Expression.Parameter(typeof(TSource), "sourceObj");
    Expression invokedSelector = Expression.Invoke(selector, new Expression[] { param });
    Expression invokedPredicate = Expression.Invoke(predicator, new[] { invokedSelector });

    return Expression.Lambda<Func<TSource, bool>>(invokedPredicate, new[] { param });
}

使用方法如下:

static void Main()
{
    Expression<Func<EntityA, EntityB>> selector = c => c.EntityB;
    Expression<Func<EntityB, bool>> predicate = c => c.IsDeleted && c.Name == "AAA";

    Expression<Func<EntityA, bool>> exp = Compose(selector, predicate);
    System.Console.WriteLine(exp.Compile()(new EntityA()));
}
© www.soinside.com 2019 - 2024. All rights reserved.