在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. 转 How to Resolve ORA-16009: remote archive log destination must be a STANDBY

    ###sample A primary B STANDBY C STANDBY   问题A 库一直报错 ORA-16009: remote archive log destination must b ...

  2. docker build doris-0.11.20-release source code

    1. pull doris dev docker image sudo docker pull apachedoris/doris-dev:build-env-1.1 2. dowload doris ...

  3. Flink FileSink 自定义输出路径——BucketingSink

    今天看到有小伙伴在问,就想着自己实现一下. 问题: Flink FileSink根据输入数据指定输出位置,比如讲对应日期的数据输出到对应目录 输入数据: 20190716 输出到路径 20190716 ...

  4. Java extract amplitude array from recorded wave

    转载自:http://ganeshtiwaridotcomdotnp.blogspot.com/2011/12/java-extract-amplitude-array-from.html Extra ...

  5. HLSFFmpegBuilder适用于hls协议的构造器 没具体测试

    import com.google.common.collect.ImmutableList; import net.bramp.ffmpeg.builder.FFmpegBuilder; impor ...

  6. LODOP中设置设置图片平铺水印,超文本透明

    之前的博文:LODOP中平铺图片 文本项Repeat. 该博文中是平铺的图片,上面是文本.如果是图片add_print_image和add_print_text纯文本,这两个打印项设计的,可以直接通过 ...

  7. LODOP打印超文本字符串拼接2 单选选择css样式表格

    之前的相关字符串拼接的博文:LODOP打印超文本字符串拼接1 固定表格填充数值之前博文介绍过,字符串可以随意拼接,只要最后组织成的字符串是自己需要的超文本就可以了,前面还有一篇也是拼接样式的:Lodo ...

  8. MVC4笔记 RedirectResult,RedirectToRoute

    RedirectResult:运行重新导向到其他网址,在RedirectResult的内部,基本上还是以Response.Redirect方法响应HTTP 302暂时导向. eg: public Ac ...

  9. xray写POC踩坑

    错误记录 静态文件目录不一定是static. 只考虑了linux的情况,如果是 windows 呢,能读取某些应用自己的源码吗. 实际环境参数不一定是id,thinkphp 不适合使用 poc 来写 ...

  10. 通过git命令“六步”提交新项目到码云

    通过git命令“六步”提交新项目到码云 一.初始化本地仓库 git init 二.添加文件 git add . 三.添加远程数据仓库链接 git remote add origin https://g ...