组合表达式(Expression<Func<TIn,TOut>> 与 Expression<Func<TOut, bool>>)

Posted

技术标签:

【中文标题】组合表达式(Expression<Func<TIn,TOut>> 与 Expression<Func<TOut, bool>>)【英文标题】:Combine Expression (Expression<Func<TIn,TOut>> with Expression<Func<TOut, bool>>) 【发布时间】:2019-11-26 06:36:07 【问题描述】:

我有两种表达方式:

Expression<Func<T1, T2>> 
Expression<Func<T2, bool>> 

我想将这些组合起来,得到一个 Expression> 类型的新表达式,以便它可以在 Entity Framework LINQ 中使用。

我在 Expression.Invoke() 的帮助下将它们组合起来,但它不起作用。

//Extensinon method
public static Expression<Func<T1, bool>> Compose<T1, T2>(this Expression<Func<T1, T2>> convertExpr, Expression<Func<T2, bool>> predicate) 
    => Expression.Lambda<Func<T1, bool>>(Expression.Invoke(predicate, convertExpr.Body), convertExpr.Parameters.First());
...
Expression<Func<One, Two>> convert;
Expression<Func<Two, bool>> predicate;
Expression<Func<One, bool>> filter=convert.Compose(predicate);

// Works fine
List<One> lst;
lst.AsQueryable().Where(filter);

DbSet<One> src;
// In next line throws runtime NotSupportedException: The LINQ expression node type 'Invoke' is not supported in LINQ to Entities
src.Where(filter);

更新

例如:

public class Book
 
  public int Id get; protected set;
  public string Name get; protected set;
  public virtual Genre get; protected set;


public class Genre

  public int Id get; protected set;
  public string Name get; protected set;

...
Expression<Func<Book, Genre>> convert = b=>b.Genre;
Expression<Func<Genre, bool>> predicate = g=>g.Name=="fantasy";
// Need: b=>b.Genre=="fantasy"
Expression<Func<Genre, bool>> filter=convert.Compose(predicate);

// Works fine
List<Book> lst;
lst.AsQueryable().Where(filter);

DbSet<Book> books;
// In next line throws runtime NotSupportedException: The LINQ expression node type 'Invoke' is not supported in LINQ to Entities
books.Where(filter);

【问题讨论】:

您是否得到错误或错误的结果?请描述你的输出。 我收到运行时错误:System.NotSupportedException:LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke” 我对类似问题的评论:我在完全外连接代码中遇到了类似的问题 - 它为完全外连接构建表达式树,在 LINQ to SQL 中运行良好,但不适用于 EF。我构建了一个ExpressionVisitor,它本质上是LINQKit 的Invoke/Expand 的有限版本,称为Apply,它就地扩展了LambdaExpression,你可以看到here。请注意,当null 是一个参数时,这个特定的Applynull 传播有特殊的考虑(静态处理null.member -> null,就好像它是null?.member)。 【参考方案1】:

使用常见的ExpressionVisitor 将一个Expression 替换为另一个,我的标准Compose 函数(我认为以更常见的数学顺序)将一个LambdaExpressionBody 替换为参数另一个:

public static class ExpressionExt 
    // Compose: (y => f(y)).Compose(x => g(x)) -> x => f(g(x))
    /// <summary>
    /// Composes two LambdaExpression into a new LambdaExpression
    /// </summary>
    /// <param name="Tpg">Type of parameter to gFn, and type of parameter to result lambda.</param>
    /// <param name="Tpf">Type of result of gFn and type of parameter to fFn.</param>
    /// <param name="TRes">Type of result of fFn and type of result of result lambda.</param>
    /// <param name="fFn">The outer LambdaExpression.</param>
    /// <param name="gFn">The inner LambdaExpression.</param>
    /// <returns>LambdaExpression representing outer composed with inner</returns>
    public static Expression<Func<Tpg, TRes>> Compose<Tpg, Tpf, TRes>(this Expression<Func<Tpf, TRes>> fFn, Expression<Func<Tpg, Tpf>> gFn) =>
        Expression.Lambda<Func<Tpg, TRes>>(fFn.Body.Replace(fFn.Parameters[0], gFn.Body), gFn.Parameters[0]);

    /// <summary>
    /// Replaces an Expression (reference Equals) with another Expression
    /// </summary>
    /// <param name="orig">The original Expression.</param>
    /// <param name="from">The from Expression.</param>
    /// <param name="to">The to Expression.</param>
    /// <returns>Expression with all occurrences of from replaced with to</returns>
    public static Expression Replace(this Expression orig, Expression from, Expression to) => new ReplaceVisitor(from, to).Visit(orig);


/// <summary>
/// ExpressionVisitor to replace an Expression (that is Equals) with another Expression.
/// </summary>
public class ReplaceVisitor : ExpressionVisitor 
    readonly Expression from;
    readonly Expression to;

    public ReplaceVisitor(Expression from, Expression to) 
        this.from = from;
        this.to = to;
    

    public override Expression Visit(Expression node) => node == from ? to : base.Visit(node);

有了这个,你的例子就很简单了:

Expression<Func<One, Two>> convert = p1 => new Two(p1);
Expression<Func<Two, bool>> predicate = p2 => p2 == new Two();
Expression<Func<One, bool>> filter = predicate.Compose(convert);

然而,尤其是对于 EF 表达式,如果您的参数可能是 null,最好使用我的替换 Invoke,它处理 null 传播。它也使用与上面相同的ReplaceExpressionVisitor

public static class ExpressionExt2    
    public static Expression PropagateNull(this Expression orig) => new NullVisitor().Visit(orig);

    // Apply: (x => f).Apply(args)
    /// <summary>
    /// Substitutes an array of Expression args for the parameters of a lambda, returning a new Expression
    /// </summary>
    /// <param name="e">The original LambdaExpression to "call".</param>
    /// <param name="args">The Expression[] of values to substitute for the parameters of e.</param>
    /// <returns>Expression representing e.Body with args substituted in</returns>
    public static Expression Apply(this LambdaExpression e, params Expression[] args) 
        var b = e.Body;

        foreach (var pa in e.Parameters.Zip(args, (p, a) => (p, a)))
            b = b.Replace(pa.p, pa.a);

        return b.PropagateNull();
    


/// <summary>
/// ExpressionVisitor to replace a null.member Expression with a null
/// </summary>
public class NullVisitor : System.Linq.Expressions.ExpressionVisitor 
    public override Expression Visit(Expression node) 
        if (node is MemberExpression nme && nme.Expression is ConstantExpression nce && nce.Value == null)
            return Expression.Constant(null, nce.Type.GetMember(nme.Member.Name).Single().GetMemberType());
        else
            return base.Visit(node);
    


public static class MeberInfoExt 
    public static Type GetMemberType(this MemberInfo member) 
        switch (member) 
            case FieldInfo mfi:
                return mfi.FieldType;
            case PropertyInfo mpi:
                return mpi.PropertyType;
            case EventInfo mei:
                return mei.EventHandlerType;
            default:
                throw new ArgumentException("MemberInfo must be if type FieldInfo, PropertyInfo or EventInfo", nameof(member));
        
        

鉴于Apply,您的Compose 很简单:

public static Expression<Func<T1, bool>> Compose<T1, T2>(this Expression<Func<T1, T2>> convertExpr, Expression<Func<T2, bool>> predicate)
    => Expression.Lambda<Func<T1, bool>>(predicate.Apply(convertExpr.Body), convertExpr.Parameters.First());

【讨论】:

哇!正是我需要的!非常感谢! 太棒了。你是怎么想到的?您是否有任何资源可以帮助更好地理解表达式树? @proximab *** 是一个很好的工具。我还使用 LINQPad 并从 lambdas 创建 Expression 变量,并使用 Dump 工具输出编译器创建的树。我有时也会使用.Net/.Net Core 源浏览器。

以上是关于组合表达式(Expression<Func<TIn,TOut>> 与 Expression<Func<TOut, bool>>)的主要内容,如果未能解决你的问题,请参考以下文章

Lambda 表达式以及如何组合它们?

如何在不使用 Invoke 方法的情况下组合两个 lambda 表达式?

如何组合两个 lambdas [重复]

将 List<Expression<Func<T, bool>>> 组合到 OR 子句 LINQ [重复]

Expression<Func<T,TResult>>和Func<T,TResult>

如何从属性名称创建 Expression<Func<TModel, string>> 表达式