C# 两个List 比较(交集、差集、并集)

在开发中对比两个集合的操作也是常有的。最近我又用上了。在这里分享一下。

交集、差集、并集

复制代码
List<int> list1 = new List<int>() { 1, 3, 5, 7, 9 };
List<int> list2 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//求差集 求不同
var exceptList = list2.Except(list1).ToList();
Console.WriteLine("求差集:" + string.Join(",", exceptList));

//求交集 求相同
var intersectList = list2.Intersect(list1).ToList();
Console.WriteLine("求交集:" + string.Join(",", intersectList));

//求并集 将两个集合的数据合并到一起
var unionList = list2.Union(list1).ToList();
Console.WriteLine("求并集:" + string.Join(",", unionList));

Console.ReadLine();
相关推荐
hhb_6186 小时前
Dylan 语言核心特性与工程实践深度解析
开发语言·c#
CSharp精选营7 小时前
最新.NET新手入门学习网站合集(2026更新版)
c#·学习资料·开发教程·.net 新手入门·开放资源·.net网站
hhb_61811 小时前
C#高性能异步编程实战与底层原理深度解析
开发语言·c#
beyond谚语11 小时前
反射、特性和依赖注入
c#
Tiger_shl12 小时前
C# 托管对象、非托管对象 讲解
开发语言·c#
LF男男12 小时前
Action- C# 内置的委托类型
java·开发语言·c#
2501_9307077816 小时前
使用C#代码在 PowerPoint 中创建组合图表
开发语言·c#·powerpoint
Full Stack Developme18 小时前
Hutool DFA 教程
开发语言·c#
xiaoshuaishuai819 小时前
【无标题】
开发语言·windows·c#
SunnyDays101119 小时前
C# 如何快速比较 Word 文档并显示差异
c#·对比 word 文档·比较 word 文档