How to chain OR clauses, with LINQ?

前端 未结 1 1505
遥遥无期
遥遥无期 2021-01-13 10:54

I\'ve got a scenario where I need to build a SQL query, using LINQ, that includes a variable amount of OR clauses. I\'m writing a function that will build the query based on

相关标签:
1条回答
  • 2021-01-13 11:24

    You can use the PredicateBuilder to chain or conditions.

    var predicate = PredicateBuilder.False<SomeEntity>();
    
    predicate = predicate.Or (p => p.A == true);
    if(something)
       predicate = predicate.Or (p => p.B == true);
    
    var query = entities.AsExpandable().Where (predicate); //AsExpandable() for EF
    
    0 讨论(0)
提交回复
热议问题