C#中如何用Intersect处理嵌套集合的交集问题


在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}。


上一篇:PHP如何在多维数组中查找元素

下一篇:PHP中使用array_search()为什么要考虑严格模式


C#
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器