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

前端 未结 5 1702
南笙
南笙 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:47

    This is the same answer I gave here

    As Marc Gravell said it involves expression-tree combining.

    This article shows you how to do that. It takes a bit of work to initially set it up. But its worth it.

    Alternate solution is to use Predicate Builder. The article does not explain very well what is actually happening under-the-hood. But the above article explains it nicely

提交回复
热议问题