C# 在C linq中查找多个列表中的公共项
我搜索了一下,但只找到了与两个列表相关的答案。但是当他们超过两个时呢C# 在C linq中查找多个列表中的公共项,c#,linq,C#,Linq,我搜索了一下,但只找到了与两个列表相关的答案。但是当他们超过两个时呢 List 1 = 1,2,3,4,5 List 2 = 6,7,8,9,1 List 3 = 3,6,9,2,0,1 List 4 = 1,2,9,0,5 List 5 = 1,7,8,6,5,4 List 6 = 1 List 7 = 如何获取常用项目?正如您所看到的,其中一个是空的,因此公用项将是空的,但我需要跳过空列表。您可以链接相交: 您可以链相交: 一种方法是使用哈希集。您可以将第一个集合的项放入散列,然后在第一个
List 1 = 1,2,3,4,5
List 2 = 6,7,8,9,1
List 3 = 3,6,9,2,0,1
List 4 = 1,2,9,0,5
List 5 = 1,7,8,6,5,4
List 6 = 1
List 7 =
如何获取常用项目?正如您所看到的,其中一个是空的,因此公用项将是空的,但我需要跳过空列表。您可以链接相交:
您可以链相交:
一种方法是使用哈希集。您可以将第一个集合的项放入散列,然后在第一个集合之后迭代每个集合,并创建一个新的散列,如果当前集合的项在散列中,则将其添加到该散列中。最后,将该公共哈希集分配给整个哈希集,如果每个哈希集都为空,则将其中断。最后,您只需返回整个哈希集
public IEnumerable<T> CommonItems<T>(IEnumerable<IEnumerable<T>> collections)
{
if(collections == null)
throw new ArgumentNullException(nameof(collections));
using(var enumerator = collections.GetEnumerator())
{
if(!enumerator.MoveNext())
return Enumerable<T>.Empty();
var overall = new HashSet<T>(enumerator.Current);
while(enumerator.MoveNext())
{
var common = new HashSet<T>();
foreach(var item in enumerator.Current)
{
if(hash.Contains(item))
common.Add(item);
}
overall = common;
if(overall.Count == 0)
break;
}
return overall;
}
}
一种方法是使用哈希集。您可以将第一个集合的项放入散列,然后在第一个集合之后迭代每个集合,并创建一个新的散列,如果当前集合的项在散列中,则将其添加到该散列中。最后,将该公共哈希集分配给整个哈希集,如果每个哈希集都为空,则将其中断。最后,您只需返回整个哈希集
public IEnumerable<T> CommonItems<T>(IEnumerable<IEnumerable<T>> collections)
{
if(collections == null)
throw new ArgumentNullException(nameof(collections));
using(var enumerator = collections.GetEnumerator())
{
if(!enumerator.MoveNext())
return Enumerable<T>.Empty();
var overall = new HashSet<T>(enumerator.Current);
while(enumerator.MoveNext())
{
var common = new HashSet<T>();
foreach(var item in enumerator.Current)
{
if(hash.Contains(item))
common.Add(item);
}
overall = common;
if(overall.Count == 0)
break;
}
return overall;
}
}
您可以对两个列表使用Intersect,然后将Intersect应用于新列表和上一个Intersect的结果等。如果您需要跳过空列表,因为您不希望它们导致结果为空,您可以只执行列表。其中l=>l.Any将其过滤掉。我更新了我的答案。它现在可以跳过空列表和null。您可以对两个列表使用Intersect,然后将Intersect应用于新列表和上一个Intersect的结果等。如果您需要跳过空列表,因为您不希望它们导致结果为空,您可以只执行lists。其中l=>l.Any将其过滤掉。我更新了我的答案。现在可以跳过空列表和null。如何跳过没有项目的列表?如何跳过没有项目的列表?枚举IEnumerable两次是一个坏习惯。不能保证每次都会得到相同的序列,但您的算法依赖于Skip1中的序列。甚至不能保证序列可以枚举两次。最后,在某些情况下,多重枚举可能非常昂贵。@AntonínLejsek True,因此这里有一个版本使用枚举器代替。枚举IEnumerable两次是一个坏习惯。不能保证每次都会得到相同的序列,但您的算法依赖于Skip1中的序列。甚至不能保证序列可以枚举两次。最后,在某些情况下,多重枚举可能非常昂贵。@AntonínLejsek True,因此这里有一个版本使用枚举器。这是最简单、最通用的答案,正确地使用Linq,并应用于列表集合,而不是像许多其他答案所给出的那样,只应用于固定数量的列表。这应该是公认的答案。这是最简单和最通用的答案,正确地使用Linq,并适用于列表集合,而不是像许多其他答案那样只适用于固定数量的列表。这应该是公认的答案。
var data = new List<List<int>> {
new List<int> {1, 2, 3, 4, 5},
new List<int> {6, 7, 2, 8, 9, 1},
new List<int> {3, 6, 9, 2, 0, 1},
new List<int> {1, 2, 9, 0, 5},
new List<int> {1, 7, 8, 6, 2, 5, 4},
new List<int> {1, 7, 2}
};
List<int> res = data
.Aggregate<IEnumerable<int>>((a, b) => a.Intersect(b))
.ToList();
List<int> res = data
.AsParallel<IEnumerable<int>>()
.Aggregate((a, b) => a.Intersect(b))
.ToList();
List<int> res = data
.AsParallel()
.Aggregate((a, b) => a.Intersect(b).ToList());
public IEnumerable<T> CommonItems<T>(IEnumerable<IEnumerable<T>> collections)
{
if(collections == null)
throw new ArgumentNullException(nameof(collections));
using(var enumerator = collections.GetEnumerator())
{
if(!enumerator.MoveNext())
return Enumerable<T>.Empty();
var overall = new HashSet<T>(enumerator.Current);
while(enumerator.MoveNext())
{
var common = new HashSet<T>();
foreach(var item in enumerator.Current)
{
if(hash.Contains(item))
common.Add(item);
}
overall = common;
if(overall.Count == 0)
break;
}
return overall;
}
}