相关疑难解决方法(0)

使用IEnumerable.Intersect()交叉多个列表

我有一个列表列表,我想找到这样的交集:

var list1 = new List<int>() { 1, 2, 3 };
var list2 = new List<int>() { 2, 3, 4 };
var list3 = new List<int>() { 3, 4, 5 };
var listOfLists = new List<List<int>>() { list1, list2, list3 };

// expected intersection is List<int>() { 3 };
Run Code Online (Sandbox Code Playgroud)

有没有办法用IEnumerable.Intersect()做到这一点?

编辑:我应该更清楚这一点:我真的有一个列表列表,我不知道会有多少列表,上面的三个列表只是一个例子,我所拥有的实际上是一个 IEnumerable<IEnumerable<SomeClass>>

谢谢你的所有好评.事实证明,解决这个问题有四种选择:List +聚合(@Marcel Gosselin),List + foreach(@JaredPar,@ Gabe Moothart),HashSet +聚合(@jesperll)和HashSet + foreach(@Tony the Pony).我对这些解决方案进行了一些性能测试(不同数量的列表,每个列表中的元素数量随机数最大值. …

.net c# linq

76
推荐指数
5
解决办法
3万
查看次数

如何交叉两个不同的IEnumerable集合

我认为这个问题之前已经被问到,但我还没有能够推断出一个明确的答案.我试图找到最好的方式(或一种方式)来交叉两个完全不同的可相关集合.

A类:

  • int z1
  • int z2
  • int z3
  • 字符串z4

B级:

  • int j5
  • int j6
  • T j7
  • T j8
  • 字符串j9

..我想相交List<A>List<B>z2 == j6.

可以这样做吗?

c# linq generics collections intersection

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×2

linq ×2

.net ×1

collections ×1

generics ×1

intersection ×1