How to make a compound “or” clause in Linq?

前端 未结 5 1678
南笙
南笙 2021-01-05 23:00

If you\'re adding \"and\" conditions to a Linq query, it\'s easy to do it like so:

var q = MyTable;
if (condition1)
  q = q.Where(t => t.Field1 == value1)         


        
5条回答
  •  借酒劲吻你
    2021-01-05 23:38

    var q = MyTable;
    var conditions = new List>();
    
     if (condition1)
         conditions.Add(t => ...);
     if (condition2)
         conditions.Add(t => ...);
    
     q.Where(x => conditions.Any(y => y(x)));
    

提交回复
热议问题