我们经常会用到linq 来查询 一个数组中和另一个数组中相同的项, 这个时候就会用到IEqualityComparer接口。

public class StudyInfoModel
{
     public string InstanceUID = "";
}
public class StudyCompare : IEqualityComparer<StudyInfoModel>
                {
        // StudyInfoModel are equal if their UID equal.
        public bool Equals(StudyInfoModel x, StudyInfoModel y)
        {
            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;
            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;
            //Check whether the Study' properties are equal.
            return x.InstanceUID == y.InstanceUID;
        }
        // If Equals() returns true for a pair of objects
        // then GetHashCode() must return the same value for these objects.
        public int GetHashCode(StudyInfoModel study)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(study, null)) return 0;
            //Get hash code for the Name field if it is not null.
            int hashStudyName = study.InstanceUID == null ? 0 : study.InstanceUID.GetHashCode();
            return hashStudyName;
        }
                }
以上实现了关于StudyInfoModel的IEqualityCompare接口, 其中条件为InstanceUID相同。
具体使用:
List<StudyInfoModel> StudyData = new List<StudyInfoModel>();
List<StudyInfoModel> StudyList = new List<StudyInfoModel>();

.......

//取得StudyData数组中InstanceUID在StudyLIst存在的所有项
var needModifyStudy = StudyData.Intersect(StudyList, new StudyCompare()).ToList();
比如StudyData中是InstanceUID分别为1,2,3,4,5,6的数据, StudyList中是InstanceUID分别为2,4,6,8,10的数据, 
则查询结果为StudyData中2,4,6的项。

C# Linq 取得两个列表的交集的更多相关文章

  1. c#得出两个列表的交集

    c#提供了Intersect来得到两个列表的交集,它是通过使用默认的相等比较器对值进行比较生成两个序列的交集,定义为: public static IEnumerable<TSource> ...

  2. LINQ获取两个List的交集

    1.调用: UserList = UserList.ToList().Intersect(userIDList, new MyUserComparer()).AsQueryable(); 2.须要重写 ...

  3. python求两个列表的并集.交集.差集

    求两个列表的差集 >>> a = [1,2,3] >>> b=[1,2] >>> ################################ ...

  4. C# Linq获取两个List或数组的差集交集

      List<); list1.Add(); list1.Add(); List<); list2.Add(); list2.Add(); //得到的结果是4,5 即减去了相同的元素. L ...

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

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

  6. [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

  7. 领扣(LeetCode)两个列表的最小索引总和 个人题解

    假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...

  8. leetcode 986. 区间列表的交集

    问题描述 给定两个由一些 闭区间 组成的列表,每个区间列表都是成对不相交的,并且已经排序. 返回这两个区间列表的交集. (形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合, ...

  9. 取两个DataTable的交集,删除重复数据

    /// <summary> /// 取两个DataTable的交集,删除重复数据 /// </summary> /// <param name="sourceD ...

随机推荐

  1. Zabbix-20160817-高危SQL注入漏洞

    漏洞概述: zabbix是一个开源的企业级性能监控解决方案.近日,zabbix的jsrpc的profileIdx2参数存在insert方式的SQL注入漏洞,攻击者无需授权登陆即可登陆zabbix管理系 ...

  2. 11-利用session校验图片认证码

    /****************************************************************产生随机验证码的servlet******************** ...

  3. centOS解决乱码问题

    问题描述:输入javac出现乱码,部分字符不能显示解决方法 echo 'export LANG=en_US.UTF-8' >> ~/.bashrc

  4. Maven中央仓库地址(实用版)

    最近做项目的时候,一直发现常用的oschina maven源一直都没有反应,后面发现原来oschina竟然关闭了maven源服务,后面经同事推荐了阿里云的maven源,这速度杠杠的 Maven 中央仓 ...

  5. 【BZOJ3435】[Wc2014]紫荆花之恋 替罪点分树+SBT

    [BZOJ3435][Wc2014]紫荆花之恋 Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从 ...

  6. struct对齐

    1 基本数据类型的自然对齐 就是说,基本数据类型的变量不能随便放在内存的任意位置,它们的起始地址必须被它们的大小整除. double是8个字节,float,int,enum是4字节,bool.char ...

  7. mysql系列之8.mysql高可用 (mha4mysql)

    环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...

  8. Grasswire&quot;草根连线&quot;:Pinterest图片流+Reddit众包新闻门户

    移动互联网技术对新闻媒体行业带来的变化是惊人的. 从专业的综合门户到维基.博客.微博.自媒体,新闻越来越散户化,众包化,也更具实时性和社交属性. Grasswire:姑且翻译为"草根连线&q ...

  9. ABAP alv report

    *&---------------------------------------------------------------------* *& Report  YTST_FF_ ...

  10. 【zabbix】自动注册,实现自动发现agent并添加监控(agent不需要任何配置)

    更新: 后来在实际使用中发现,与其使用zabbix自动注册,不如直接调用zabbix的api主动发起添加服务器的请求,这样就不需要在zabbixserver上配置host信息了.实现全自动.具体调用方 ...