在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方法求交集的更多相关文章

  1. 编程中遇到的Python错误和解决方法汇总整理

    这篇文章主要介绍了自己编程中遇到的Python错误和解决方法汇总整理,本文收集整理了较多的案例,需要的朋友可以参考下   开个贴,用于记录平时经常碰到的Python的错误同时对导致错误的原因进行分析, ...

  2. 转载:C++中两个类中互相包含对方对象的指针问题

    原文链接:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html 前几天很不爽,因为C++中两个类中互相包含对方对象的指针编译时提示某 ...

  3. 【转载】C#中double.TryParse方法和double.Parse方法的异同之处

    在C#编程过程中,double.TryParse方法和double.Parse方法都可以将字符串string转换为double类型,但两者还是有区别,最重要的区别在于double.TryParse方法 ...

  4. 【转载】 C#中decimal.TryParse方法和decimal.Parse方法的异同之处

    在C#编程过程中,decimal.TryParse方法和decimal.Parse方法都可以将字符串string转换为decimal类型,但两者还是有区别,最重要的区别在于decimal.TryPar ...

  5. 【转载】C#中float.TryParse方法和float.Parse方法的异同之处

    在C#编程过程中,float.TryParse方法和float.Parse方法都可以将字符串string转换为单精度浮点类型float,但两者还是有区别,最重要的区别在于float.TryParse方 ...

  6. 【转载】C#中int.TryParse方法和int.Parse方法的异同之处

    在C#编程过程中,int.TryParse方法和int.Parse方法都可以将字符串string转换为整型int类型,但两者还是有区别,最重要的区别在于int.TryParse方法在字符串无法转换为i ...

  7. (转载)java中判断字符串是否为数字的方法的几种方法

    java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...

  8. 转载---class文件中的字段表集合--field字段在class文件中是怎样组织的

    写的太好了! https://blog.51cto.com/1459294/1932331

  9. IOS 编程中引用第三方的方类库的方法及常见问题

    方法一:直接复制全部源文件到项目中 这样的方法就是把第三方类库的全部源文件拷贝到项目中,直接把全部.h和.m文件拖到XCode项目中就可以. 注意: 1. 假设第三方类库引用了一些系统自带类库,那么在 ...

随机推荐

  1. Leetcode: Shortest Way to Form String

    From any string, we can form a subsequence of that string by deleting some number of characters (pos ...

  2. 008-MySQL报错-Access denied for user 'root'@'localhost' (using password: NO)

    1.新安装的mysql报错 MySQL报错-Access denied for user 'root'@'localhost' (using password: NO) 解决方案 1.先停掉原来的服务 ...

  3. DataGrip 2019.1 连接mysql 8.0.16

    # 下载mysql Connector/J驱动包 https://dev.mysql.com/downloads/connector/j/ 然后解压到一个目录 # 新建mysql 8.0连接驱动 打开 ...

  4. VMware Workstation 将虚拟机挂起后,电脑会很卡,SCSI转换成IDE就可以了

    摘自:http://www.360doc.com/content/15/0405/09/10098873_460727712.shtml 用过 VMware Workstation 的人,不知道有没有 ...

  5. Vue cli4.0 代理配置

    proxy: { '/service': { target: 'http://192.168.40.243:3000/', //对应自己的接口 changeOrigin: true, ws: true ...

  6. MySQL 8中使用全文检索示例

    首先建议张册测试用的表test,并使用fulltext说明将title和body两列的数据加入全文检索的索引列中: drop table if exists test; create table te ...

  7. PyCharm安装及其使用

    1.前提:Python+selenium的安装教程如下网址 https://www.cnblogs.com/linxiu-0925/p/9597634.html 2.PyCharm安装 1.首先去Py ...

  8. autocomplete="off" 在新版chrome中不起作用

    autocomplete="off" 在新版chrome中不起作用,还是自动填充了 用户名和密码,改为 autocomplete="new-password"  ...

  9. package ‘RPMM’ is not available (for R version 3.6.0)

    当我们用启动R安装一些R包的时候 提示: Warning: unable to access index for repository https://mirrors.eliteu.cn/CRAN/s ...

  10. ubuntu 安装 typora

    # or run: # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA300B7755AFCFAE wget -qO ...