LINQ WHERE statement/ignore conditions

我怕爱的太早我们不能终老 提交于 2019-12-03 13:18:17

You can add it as a condition:

 var query= from x in context.a 
            where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
            select x;

If param1 is null or empty, the condition will always be true, which effectively "ignores" the where conditions entirely.

you can individually check param1 and param2 this one..

var query = from x in context.a
             where (X.p==param1 || string.IsNullOrEmpty(param1))
             && (X.i==param2 || string.IsNullOrEmpty(param2))
             select x;

the above two conditions also works well , if you want to check individually

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