本文介绍了两个集合中的任何交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须找出两个集合是否有交集,我这样做的方式是使用LINQ的加入获得两个集合的交集,然后我使用任何。但我想知道,是否还有其他更优雅的方式这样做?解决方案
可能是你在看
从MSDN:
int [] id1 = { 44,26,92,30,71,38};
int [] id2 = {39,59,83,47,26,4,30};
IEnumerable< int> both = id1.Intersect(id2);
if(both.Any())...
i have to find out whether or not two collections have any intersection, the way that i did that is using LINQ's "Join" to get the Intersection of the two collections and then i use "Any". But i wonder, is there other more "elegant" way of doing this?
解决方案
Enumerable.Intersect
is probably what you're looking for.
From MSDN:
int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };
IEnumerable<int> both = id1.Intersect(id2);
if(both.Any())...
这篇关于两个集合中的任何交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!