【转载】C#编程中两个List集合使用Intersect方法求交集
在C#语言程序设计中,List集合是常用的集合数据类型,在涉及集合类型的运算中,有时候我们需要计算2个List集合中共有的数据,即对2个List集合求交集运算。此时可以使用C#语言提供的Intersect方法快速来实现两个集合之间的交集运算。Except方法调用的格式为:List1.Intersect(List2),List1和List2是相同类型的List集合数据,求出交集数据后可再使用ToList方法转换回List集合类型数据。
例如下列两个集合都为List<int>集合,list1包含的数据有: 1, 2, 3, 4, 5, 6, 7 。list2包含的数据有:4, 5, 6, 7, 8, 9, 10。针对list1和list2求交集可使用以下语句:
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
List<int> list2 = new List<int>() { 4, 5, 6, 7, 8, 9, 10 };
List<int> jiaojiList = list1.Intersect(list2).ToList();//使用Intersect方法求交集运算。
最后得到的结果集合jiaojiList中的数据为:4,5,6,7。
上述语句只针对普通基础的值类型,如果针对自定义类的对象的话,如果要求交集运算,则是需要相同的对象引用才算是交集中的数据,而非两个对象相同即是交集。
下面以2个例子来阐述上述加粗这部分的含义,首先我们定义要使用测试的类为TestModel类,类中只有简单的Index和Name属性,具体结构如下:
public class TestModel
{
public int Index { set; get; } public string Name { set; get; }
}
(1)例子1:list1集合和list2集合中的对象元素分别来之不同的对象引用,这list1和list2交集元素个数为0。
List<TestModel> list1 = new List<TestModel>();
list1.Add(new TestModel() { Index = 1, Name = "TestModel1" });
list1.Add(new TestModel() { Index = 2, Name = "TestModel2" });
list1.Add(new TestModel() { Index = 3, Name = "TestModel3" });
list1.Add(new TestModel() { Index = 4, Name = "TestModel4" }); List<TestModel> list2 = new List<TestModel>();
list2.Add(new TestModel() { Index = 3, Name = "TestModel3" });
list2.Add(new TestModel() { Index = 4, Name = "TestModel4" });
list2.Add(new TestModel() { Index = 5, Name = "TestModel5" });
list2.Add(new TestModel() { Index = 6, Name = "TestModel6" });
从运行的结果来看:jiaojiList为空集合,里面没有任何对象。
(2)例子2:list1集合和list2集合中的部分元素的对象引用是一致的时候,list1和list2交集的元素为这些相同对象引用的数据。
List<TestModel> list1 = new List<TestModel>();
List<TestModel> list2 = new List<TestModel>(); TestModel model1 = new TestModel() { Index = 1, Name = "TestModel1" };
TestModel model2 = new TestModel() { Index = 2, Name = "TestModel2" };
TestModel model3 = new TestModel() { Index = 3, Name = "TestModel3" };
TestModel model4 = new TestModel() { Index = 4, Name = "TestModel4" }; list1.Add(model1);
list1.Add(model2);
list1.Add(model3); list2.Add(model2);
list2.Add(model3);
list2.Add(model4); List<TestModel> jiaojiList = list1.Intersect(list2).ToList();
从运行的结果来看:jiaojiList含有2个元素,分别为model2和model3。
关于对象集合的交集的运算,例子1和例子2的差别读者详细斟酌下即可理解,是以对象的引用来判断的,而非对象的每个属性值都相同来判断,针对于自定义类对象的List集合求交集的时候需要注意这些事项。
备注:原文转载自博主个人技术站点IT技术小趣屋,原文链接C#编程中两个List集合使用Intersect方法求交集_IT技术小趣屋。
博主个人技术交流群:960640092,博主微信公众号如下:

【转载】C#编程中两个List集合使用Intersect方法求交集的更多相关文章
- 编程中遇到的Python错误和解决方法汇总整理
这篇文章主要介绍了自己编程中遇到的Python错误和解决方法汇总整理,本文收集整理了较多的案例,需要的朋友可以参考下 开个贴,用于记录平时经常碰到的Python的错误同时对导致错误的原因进行分析, ...
- 转载:C++中两个类中互相包含对方对象的指针问题
原文链接:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html 前几天很不爽,因为C++中两个类中互相包含对方对象的指针编译时提示某 ...
- 【转载】C#中double.TryParse方法和double.Parse方法的异同之处
在C#编程过程中,double.TryParse方法和double.Parse方法都可以将字符串string转换为double类型,但两者还是有区别,最重要的区别在于double.TryParse方法 ...
- 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处
在C#编程过程中,decimal.TryParse方法和decimal.Parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.TryPar ...
- 【转载】C#中float.TryParse方法和float.Parse方法的异同之处
在C#编程过程中,float.TryParse方法和float.Parse方法都可以将字符串string转换为单精度浮点类型float,但两者还是有区别,最重要的区别在于float.TryParse方 ...
- 【转载】C#中int.TryParse方法和int.Parse方法的异同之处
在C#编程过程中,int.TryParse方法和int.Parse方法都可以将字符串string转换为整型int类型,但两者还是有区别,最重要的区别在于int.TryParse方法在字符串无法转换为i ...
- (转载)java中判断字符串是否为数字的方法的几种方法
java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...
- 转载---class文件中的字段表集合--field字段在class文件中是怎样组织的
写的太好了! https://blog.51cto.com/1459294/1932331
- IOS 编程中引用第三方的方类库的方法及常见问题
方法一:直接复制全部源文件到项目中 这样的方法就是把第三方类库的全部源文件拷贝到项目中,直接把全部.h和.m文件拖到XCode项目中就可以. 注意: 1. 假设第三方类库引用了一些系统自带类库,那么在 ...
随机推荐
- Flutter 页面布局 Stack层叠组件
Stack 表示堆的意思,我们可以用 Stack 或者 Stack 结合 Align 或者 Stack 结合 Positiond 来实现页面的定位布局 属性 说明 alignment 配置所有子元素的 ...
- Hashset(不能添加相同的字符进入数组)
参考: https://ke.qq.com/webcourse/index.html#cid=434021&term_id=100518216&taid=377652179413386 ...
- 123457123456#1#----com.MC.CarWashKidsGames234----前拼后广--洗车游戏mc-mc1111
com.MC.CarWashKidsGames234----前拼后广--洗车游戏mc-mc1111
- Python - Django - Cookie 简单用法
home.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- C语言中结构体的构造函数
示例代码: #include <iostream> using namespace std; struct Node { int x, y, z; Node(int _x, int _y, ...
- 安卓微信overflow-x overflow-y引发的bug
今天xgo文章图片页上线用微信扫页面发现一个bug,页面可以双击放大缩小. 找了半天原因,发现是图片描述设置了overflow-y引发的bug. 建议在微信场景里满屏显示不能滚动的页面里慎用overf ...
- 打印格式化printf
#define _DEBUG_ #ifdef _DEBUG_#define printm(fmt, ...) do { printf("%s line %d, "fmt, __f ...
- Pytorch 网络结构可视化
安装 conda install graphvizconda install tensorwatch 载入库 import sysimport torchimport tensorwatch as t ...
- [转帖]运维必读:Linux 的内存分页管理
运维必读:Linux 的内存分页管理 https://cloud.tencent.com/developer/article/1356431 内存是计算机的主存储器.内存为进程开辟出进程空间,让进程在 ...
- [转帖]时序数据库技术体系(二):初识InfluxDB
时序数据库技术体系(二):初识InfluxDB https://sq.163yun.com/blog/article/169866295296581632 把生命浪费在美好事物上2018-06-26 ...