LINQ:查询,如果集合包含另一个集合的任何元素 [英] LINQ: Query if collection contains any element in another collection
问题描述
所以,很显然,这是一对夫妇foreach循环容易可行的,但我只是用C#经过多年的Java的开始,现在我想的东西LINQ到一切,因为它是如此真棒。
我有一个字符串两个ICollections,我要检查,如果一个集合包含任何其他一个字符串。换句话说,我要检查,如果两个集合的并集是空的。
在这种情况下,我没有真正关心哪些字符串匹配,就是否存在匹配或没有。我假设任何这里的关键是方法,但是我想不出怎么做我想用它。我敢肯定,该解决方案非常简单;我只是不很熟悉的建筑查询。
如果(a.Intersect (b)。任何())
So obviously this is easily doable with a couple of foreach loops, but I just started using C# after years of Java and now I'm trying to stuff LINQ into everything because it's so awesome.
I have two ICollections of strings, and I want to check if one collection contains any of the strings in the other one. Put another way, I want to check if the union of the two collections is empty or not.
In this case I'm not actually concerned with WHICH strings match, just whether a match exists or not. I'm assuming Any is the key method here, but I can't figure out how to do what I want with it. I'm sure the solution's pretty simple; I'm just not very familiar with building queries.
if (a.Intersect(b).Any())
这篇关于LINQ:查询,如果集合包含另一个集合的任何元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!