动态LINQ OR条件 [英] Dynamic LINQ OR Conditions
本文介绍了动态LINQ OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找使用LINQ在类似集合的条件上进行多次where条件
I'm looking to use LINQ to do multiple where conditions on a collection similar to
IEnumerable<Object> items;
items.Where(p => p.FirstName = "John");
items.Where(p => p.LastName = "Smith");
除了具有多个AND条件(如本示例所示)外,我希望具有多个OR条件.
except for rather than having multiple AND conditions (as with this example), I'd like to have multiple OR conditions.
编辑 抱歉,为了澄清,我不知道会有多少这样的条件
EDIT Sorry, to clarify I don't know how many of these conditions I will have so
items.Where(p => p.FirstName = "John" || p => p.LastName = "Smith")
不起作用.
基本上,这就是我想要做的:
Basically, here's what I'm trying to do:
foreach(var name in names)
{
items = items.Where(p => p.Name == name);
}
推荐答案
听起来您的白名单仅在运行时才知道.也许试试看:
It sounds like your whitelist of names is only known at runtime. Perhaps try this:
string[] names = new string[] {"John", "foo", "bar"};
var matching = items.Where(x => names.Contains(x.Name));
这篇关于动态LINQ OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文