在C#中,可以使用LINQ的Intersect方法来处理嵌套集合的交集问题。下面是一个示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<List<int>> nestedList1 = new List<List<int>>
{
new List<int> {1, 2, 3},
new List<int> {2, 3, 4},
new List<int> {3, 4, 5}
};
List<List<int>> nestedList2 = new List<List<int>>
{
new List<int> {2, 3, 4},
new List<int> {3, 4, 5},
new List<int> {4, 5, 6}
};
var intersection = nestedList1.SelectMany(x => x)
.Intersect(nestedList2.SelectMany(y => y))
.ToList();
foreach (var item in intersection)
{
Console.WriteLine(item);
}
}
}
在这个示例中,我们有两个嵌套的List<List>,分别是nestedList1和nestedList2。我们通过SelectMany方法将它们转换成一个一维的List,然后使用Intersect方法找到它们的交集并输出。最后的输出结果将是{3, 4}。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/970586.html