C# 集合的交集 差集 并集 去重

两个对象list,直接比较是不行的,因为他们存的地址不一样

需要重写GetHashCode()与Equals(object obj)方法告诉电脑

class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
} class CompareStudent : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
return x.Id == y.Id;
} public int GetHashCode(Student p)
{
if (p == null)
return ;
return p.Id.GetHashCode();
}
} class Program
{
static void Main(string[] args)
{
List<Student> stuA = new List<Student>();
List<Student> stuB = new List<Student>();
stuA.Add(new Student { Id = , Name = "", Age = });
stuA.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
stuB.Add(new Student { Id = , Name = "", Age = });
var result = stuA.Where(a => !stuB.Exists(b => b.Id == a.Id)); //在A中存在不再B中存在 即求差集
var resc = stuA.Except(stuB, new CompareStudent()); //差集
var resj = stuA.Intersect(stuB, new CompareStudent());// 交集
var resb = stuA.Union(stuB, new CompareStudent()); //并集
var resD = stuB.Distinct(new CompareStudent()); //去重
}
}

C# 集合的交集 差集 并集 去重的更多相关文章

  1. C# 数组比较--取得两个集合的交集,差集,并集的方法

    方法关键字: 交集:Intersect 差集:Except 并集:Union 使用代码: , , , , }; , , , , }; var 交集 = arr1.Intersect(arr2).ToL ...

  2. python-->(set /dict)交集 差集 并集 补集(功能用来做交差并补的)

    # ### 集合 作用:交集 差集 并集 补集(功能用来做交差并补的) '''特征:自动去重 无序''' #定义一个空集合 setvar = set() #set()强制转换成一个空集合的数据类型 p ...

  3. scala中集合的交集、并集、差集

    scala中有一些api设计的很人性化,集合的这几个操作是个代表: 交集: scala> Set(1,2,3) & Set(2,4) // &方法等同于interset方法 sc ...

  4. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  5. 60-python基础-python3-集合-集合常用方法-交集、并集、差集、对称差集-intersection(&)-union(|)-difference(-)-symmetric_difference()

    交集.并集.差集-intersection(&)-union(|)-difference(-) 1-intersection(&) s1.intersection(s2),返回s1和s ...

  6. java集合(交集,并集,差集)

    说明:这里没有求差集的代码,有了交集和并集,差集=并集-交集       package com; import java.util.ArrayList; import java.util.HashS ...

  7. java求两个集合的交集和并集,比较器

    求连个集合的交集: import java.util.ArrayList; import java.util.List; public class TestCollection { public st ...

  8. C++求集合的交集差集

    标准库的<algorithm>头文件中提供了std::set_difference,std::set_intersection和std::set_union用来求两个集合的差集,交集和并集 ...

  9. 求两个集合的交集和并集C#

    我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...

随机推荐

  1. linux学习(十)Shell中的控制语句

    目录 1. 条件测试语句 1.1 test语句 1.2[]语句 1.3文件测试 1.4字符串测试 1.5数值测试 1.5逻辑操作符 @(Shell中的控制语句) 1. 条件测试语句 测试语句十Shel ...

  2. C#&.Net干货分享- iTextSharp导出数据源到PDF

    namespace Frame.ITextSharp{    /// <summary>    /// iTextSharp导出数据源到PDF    /// </summary> ...

  3. Django—使用后台管理Models

    后台的配置 1.创建后台管理员 [root@localhost study_django]# python manage.py createsuperuser [root@localhost stud ...

  4. Druid-代码段-1-2

    所属文章:池化技术(一)Druid是如何管理数据库连接的? 本代码段对应流程1.1,责任链的执行: //DruidDataSource类里的方法:获取连接 public DruidPooledConn ...

  5. go语言设计模式之builder

    builder.go package builder type BuildProcess interface { SetWheels() BuildProcess SetSeats() BuildPr ...

  6. 初学Python:Python的发展历史及下载安装

    Python作为一种计算机程序设计语言,自20世纪90年代初诞生至如今被人们逐渐悉知,经过版本更新以及功能添加,已广泛应用于各种独立的.大型项目的开发.Python 已经成为最受欢迎的程序设计语言之一 ...

  7. 更新GitHub项目出现There is no tracking information for the current branch. Please specify which branch you want to merge with. 怎么解决

    git pull命令用于从另一个存储库或本地分支获取并集成(整合).git pull命令的作用是:取回远程主机某个分支的更新,再与本地的指定分支合并,它的完整格式稍稍有点复杂. 如果当前分支只有一个追 ...

  8. selenium爬取NBA并将数据存储到MongoDB

    from selenium import webdriver driver = webdriver.Chrome() url = 'https://www.basketball-reference.c ...

  9. 黑科技,利用python拨打电话,控制手机技术!

    跟selenium操作浏览器原理类似,这是用appium操作移动设备的一个自动化功能,自娱自乐,主要是通过小案例引出相关技术 很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手. 很 ...

  10. ACR122U读卡器在win7以上系统使用过程中的设置项

    发现ACR122U这个读卡器在进行nested破解的时候总是卡死,换了N个驱动程序都不行. 后发现是windows系统因智能卡的即插即用设置导致的问题,可以通过组策略的设置搞定. gpedit.msc ...