LINQ WHERE with OR

橙三吉。 提交于 2019-12-05 00:55:48

PredicateBuilder is the perfect solution for your problem. It allows you to keep adding individual "AND" as well as "OR" statements together.

You could do something like:

var query = from x in context.Xs
        where
          (x.X == 1) ||
          (x.Y == 2) ||
          (x.Z == "3")
        select x;

I would suggest using Expression Trees to build your query dynamically:

(MSDN) How to: Use Expression Trees to Build Dynamic Queries

You could also use a PredicateBuilder (which does something similar under the hood) to build the Predicate dynamically and then pass the final Predicate to the Where method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!