【问题标题】:Intersect a collection of collections in LINQ与 LINQ 中的集合集合相交
【发布时间】:2010-03-28 05:30:26
【问题描述】:

我有一个要相交的列表列表:

List<List<int>> input = new List<List<int>>();
input.Add(new List<int>() { 1, 2, 4, 5, 8 });
input.Add(new List<int>() { 3, 4, 5 });
input.Add(new List<int>() { 1, 4, 5, 6 });

输出应该是:

{ 4, 5 }

如何以简洁的方式实现这一点?

【问题讨论】:

标签: c# linq


【解决方案1】:
var result = input.Cast<IEnumerable<int>>().Aggregate((x, y) => x.Intersect(y))

【讨论】:

  • 很抱歉第一次犯了错误,我忘了我必须投降到IEnumerable&lt;int&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多