c# - 在 C# 中使用动态或表达式

标签 c# linq expression

我在我的程序中使用动态过滤器,如以下解决方案:

public static IQueryable<T> MyFilter<T>(this IQueryable<T> queryable) where T : class ,IModel
{
        var someIds = new int[]{1,2,3,4,5};
        var userId = 2;

        Expression<Func<T, bool>> predicate1 = e => someIds.Contains(e.Id);
        Expression<Func<T, bool>> predicate2 = e => e.UserId==userId;
        Expression<Func<T, bool>> predicate3 = e => e.CreatedDate != null;

        var pred1 = Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.Or(predicate1, predicate2));
        var pred2 = Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.Or(pred1, predicate3));


        var result = queryable
            .Where(pred2);

        return result;
    }

IModel 是这样的:

Public interface IModel{
   int Id { get; set; }
   int UserId { get; set; }
   DateTime? CreatedDate { get; set; }
}

但是,我得到这个错误:

The binary operator Or is not defined for the types 'System.Func2[AnyClass,System.Boolean]' and 'System.Func2[AnyClass,System.Boolean]'.

在这一行:

var pred1 = Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.Or(predicate1, predicate2));

我该如何解决这个问题?

预先感谢任何帮助

最佳答案

正如我在评论中所写,存在两个不同的问题:

  • 您必须使用 Expression.OrElse,因为 Expression.Or| 运算符。

  • 但真正的问题是您不能以这种方式连接 Lambda 表达式(predicate123) .连接表达式很复杂,因为predicate123的输入参数不同(好像是e1, e2, e3 而不是 e), pred1pred2 将有一个 e4e5 输入参数,所以你必须要替换一些表达式。

解决方法:

// Note that we only need the Body!
Expression pred1 = Expression.OrElse(Expression.OrElse(predicate1.Body, predicate2.Body), predicate3.Body);

// We change all the predicate2.Parameters[0] to predicate1.Parameters[0] and
// predicate3.Parameters[0] to predicate1.Parameters[0]

var replacer = new SimpleExpressionReplacer(
    /* from */ new[] { predicate2.Parameters[0], predicate3.Parameters[0] }, 
    /* to */ new[] { predicate1.Parameters[0], predicate1.Parameters[0] });

pred1 = replacer.Visit(pred1);

// We use for the new predicate the predicate1.Parameters[0]
var pred2 = Expression.Lambda<Func<T, bool>>(pred1, predicate1.Parameters[0]);

var result = queryable.Where(pred2);

SimpleExpressionReplacer:

// A simple expression visitor to replace some nodes of an expression 
// with some other nodes
public class SimpleExpressionReplacer : ExpressionVisitor
{
    public readonly Dictionary<Expression, Expression> Replaces;

    public SimpleExpressionReplacer(Dictionary<Expression, Expression> replaces)
    {
        Replaces = replaces;
    }

    public SimpleExpressionReplacer(IEnumerable<Expression> from, IEnumerable<Expression> to)
    {
        Replaces = new Dictionary<Expression, Expression>();

        using (var enu1 = from.GetEnumerator())
        using (var enu2 = to.GetEnumerator())
        {
            while (true)
            {
                bool res1 = enu1.MoveNext();
                bool res2 = enu2.MoveNext();

                if (!res1 || !res2)
                {
                    if (!res1 && !res2)
                    {
                        break;
                    }

                    if (!res1)
                    {
                        throw new ArgumentException("from shorter");
                    }

                    throw new ArgumentException("to shorter");
                }

                Replaces.Add(enu1.Current, enu2.Current);
            }
        }
    }

    public SimpleExpressionReplacer(Expression from, Expression to)
    {
        Replaces = new Dictionary<Expression, Expression> { { from, to } };
    }

    public override Expression Visit(Expression node)
    {
        Expression to;

        if (node != null && Replaces.TryGetValue(node, out to))
        {
            return base.Visit(to);
        }

        return base.Visit(node);
    }
}

关于c# - 在 C# 中使用动态或表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29467698/

相关文章:

c# - 在 x64 平台上执行聚合查询时出现异常

c# - LINQ to SQL 表达式 "Orderby"帮助

c# - 如何使用linq过滤SelectList

c# - 检查 Dictionary 的值是否包含具有特定字段值的元素

linq - EFCore 2.2 GroupBy Sum 和 DateDiff

c# - 正则表达式包含多个字符串

c# - LINQ:显示空列表的结果

c# - 后台工作线程中异步等待的调解器死锁 - 如何检测调用自身的线程

c# - 好奇心 : Why does Expression<. ..> 编译时运行速度比最小 DynamicMethod 快?

c# - 使用反射、链式方法和 lambda 表达式动态创建对象