How to Append to an expression

后端 未结 8 1547
清歌不尽
清歌不尽 2020-12-05 01:46

Based on my question from yesterday:

if I had to append to my existing \'where\' expression, how would i append?

Expression

        
8条回答
  •  抹茶落季
    2020-12-05 02:21

    This is a complex scenario. You are almost building your own query engine on top of LINQ. JaredPar's solution (where did it go?) is great if you want a logical AND between all of your criteria, but that may not always be the case.

    When I was wrangling with this in one of my project recently, I created two Lists:

    List> andCriteria;
    List> orCriteria;
    

    (In this case, T is Client, for you)

    I would populate the Lists with predicates that I want to be true. For instance,

    decimal salRequirement = 50000.00;
    andCriteria.Add(c => c.Salary > salRequirement);
    orCriteria.Add(c => c.IsMarried);
    

    Then, I would check against all the criteria in the Lists in my Where clause. For instance:

    Expression> clientWhere =
        c => andCriteria.All(pred => pred(c) ) && orCriteria.Any(pred => pred(c) );
    

    This could also be done with a for-loop for readability's sake. Remember to use the correct order of operations when applying your OR and AND clauses.

提交回复
热议问题