LINQ Where子句中的if条件 [英] If condition in LINQ Where clause
本文介绍了LINQ Where子句中的if条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以在Linq的哪里使用if子句吗?
Can I use if clause with Linq where?
推荐答案
是的,您可以:
var query = someList.Where(a => a == "something");
if (condition)
{
query = query.Where(b => b == "something else");
}
var result = query.ToList();
由于Where
正在生成IQueryable
,因此执行被推迟到本例中的ToList
,这样您就可以将Where
尽可能多地链接在一起,然后在通过所有操作之后才执行您的条件.
Because Where
is producing an IQueryable
, the execution is deferred until the ToList
in my example so you can chain Where
s together as much as you want and then just execute it after you have passed all your conditions.
这篇关于LINQ Where子句中的if条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文