簡體   English   中英

組合兩個linq表達式

[英]combing two linq expressions

我想合並兩個linq表達式,但是我的代碼給我一個錯誤二進制運算符And並未為類型'System.Func`2[Web.Entities.Customer,System.Boolean]''System.Func`2[Web.Entities.Customer,System.Boolean]'

我有2個表達式,例如...

 Expression<Func<Customer, bool>> filter = c => c.Active;
 Expression<Func<Customer, bool>> filterz = c => c.Visible;

然后我將它們結合起來

 filter = Expression.Lambda<Func<Customer, bool>>(Expression.And(filter, filterz));

關於這個問題有幫助嗎?

謝謝....

這是下面答案中給出的更新代碼。

public class SwapVisitor : ExpressionVisitor
{

    private readonly Expression from, to;
    public SwapVisitor(Expression from, Expression to)
    {
        this.from = from;
        this.to = to;
    }

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

filter = Expression.Lambda<Func<Customer, bool>>(Expression.AndAlso(
                        new SwapVisitor(filter.Parameters[0], filterz.Parameters[0]).Visit(filter.Body), filterz.Body), filterz.Parameters)
Expression<Func<Customer, bool>> filter1 = c => c.Active;
Expression<Func<Customer, bool>> filter2 = c => c.Visible;

var parameter = Expression.Parameter(typeof(Customer), "x");

var filter = Expression.Lambda<Func<Customer, bool>>(
                Expression.AndAlso(
                    Expression.Invoke(filter1,parameter),
                    Expression.Invoke(filter2,parameter)
                ),parameter
            );

如果您在每個過濾器中都與同Customer一起工作,則可以嘗試:

        Expression<Func<Customer, bool>> filter = c => c.Active;
        Expression<Func<Customer, bool>> filter2 = c => c.Visible;

        var body = Expression.AndAlso(filter.Body, Expression.Invoke(filter2, filter.Parameters[0]));

        filter = Expression.Lambda<Func<Customer, bool>>(body, filter.Parameters);
        var applyFilter = filter.Compile();

        var customer = new Customer() { Visible = true, Active = true};
        Console.WriteLine(applyFilter(customer));

        customer.Active = false;
        Console.WriteLine(applyFilter(customer));

        customer.Visible = false;
        Console.WriteLine(applyFilter(customer));

您已經可以在存儲庫中使用表達式,而無需外部組合它們:

var yourContext = getContext();
var filtered = yourContext.Where(filter).Where(filter2);

在這種情況下,不需要合並,並且這種方法在不影響效率的情況下都會起作用。

如果需要組合:

嘗試使用以下訪客幫助器:

public class ReplacementVisitor : System.Linq.Expressions.ExpressionVisitor
{
    private readonly Expression _oldExpr;
    private readonly Expression _newExpr;
    public ReplacementVisitor(Expression oldExpr, Expression newExpr)
    {
        _oldExpr = oldExpr;
        _newExpr = newExpr;
    }

    public override Expression Visit(Expression node)
    {
        if (node == _oldExpr)
            return _newExpr;
        return base.Visit(node);
    }
}

我們需要它,因為您的過濾表達式使用不同的參數實例。 為了進行組合,我們需要它們使用相同的參數實例。 此類有助於我們執行以下操作:

Expression<Func<Customer, bool>> filter = c => c.Active;
Expression<Func<Customer, bool>> filterz = c => c.Visible;

var newParameter = Expression.Parameter(typeof(Customer), "x");
var visitor1 = new ReplacementVisitor(filter.Parameters[0], newParameter);
var visitor2 = new ReplacementVisitor(filterz.Parameters[0], newParameter);

var newLambda = Expression.Lambda(
    Expression.AndAlso(
        visitor1.Visit(filter.Body),
        visitor2.Visit(filterz.Body)
    ),
    newParameter
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM