我们经常会用到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. JS常见事件以及函数

    1.js enter键激发事件 document.onkeydown = function (e) {            if (!e) e = window.event;             ...

  2. ios - UISearchBar输入框背景色

    //输入框背景色 bar.searchBarStyle = UISearchBarStyleMinimal; [bar positionAdjustmentForSearchBarIcon:UISea ...

  3. delphi Base64编码/解码及数据压缩/解压知识

    一.Base64编码/解码 一般用到的是Delphi自带的单元EncdDecd,当然还有第三方提供的单元或控件,其中我所接触到的认为比较好的有Indy的TIdMimeEncode / TIdMimeD ...

  4. JS之字符串与JSON转换

    JS之字符串转换JSON 1.eval   古老的方式 function strToJson(str){ var json = eval('(' + str + ')'); return json; ...

  5. adaptive heuristic critic 自适应启发评价 强化学习

    https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume4/kaelbling96a-html/node24.html [旧知-新知   强化学习:对 ...

  6. 对A轮的追逐变得越加狂热,当前距离互联网泡沫到底有多近?

    编者注:本文来自TOMASZ TUNGUZ,中文版由天地会珠海分舵进行编译. 依据NVCA披露的最新数据,在2015年第二季度.VC总共进行了167亿美元的投资,大约是2000年互联网泡沫时候同期的6 ...

  7. Flask:基本结构

    python有两个比较出名的网络框架,一个是django,一个是flask. 之前的django文章里面介绍了django的各种用法,这个系列开始介绍flask的用法.相比与django,flask更 ...

  8. 硬分叉后,BCH的钱包解决方案

    上周BCH进行了硬分叉,分叉成了两条链:BCH和BCHSV,对于分叉后的BCH如何进行交易呢?钱包是否有相关的危险因素? 由于分叉后的两条链没做重放保护,可能导致一条链上发起的交易,在另一条链上做重放 ...

  9. python之学习

    ------------------------------------------  基本语句解析 import:导入某些模块或者文件 import random: 导入生成随机数模块 import ...

  10. Docker实践中遇到的坑

    1.docker容器中后台运行退出执行curl+p+q,再次进入执行命令docker attach 容器id. 2.容器中exit退出后,还原方法为docker ps -a 查看历史运行容器,dock ...