我们经常会用到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. Window下安装Gradle并在IDEA中配置

    真是作死啊,Maven都只是用得半瓢水,还来搞Gradle,小心撩得一身骚啊. 下载Gradle 下载页面为:https://gradle.org/releases/ Gradle 4.1的下载地址: ...

  2. HUAWEI HiAI亮相华为开发者生态大会 助力应用AI开发实现加速度

    6月23日,在2018华为终端·全球合作伙伴及开发者大会AI分论坛体验区的一角,被层层叠叠的人群围得水泄不通.站在最前面的一名体验者,正跟随着“快手短视频”APP上不断出现的小人左右扭动,每完成一个动 ...

  3. beego的MVC架构介绍

    beego 的 MVC 架构介绍 beego 是一个典型的 MVC 框架,它的整个执行逻辑如下图所示: 通过文字来描述如下: 在监听的端口接收数据,默认监听在 8080 端口. 用户请求到达 8080 ...

  4. MySQL常见的数据类型(八)

    不多说,直接上干货! MySQL常见的数据类型 一.数据类型是什么? 数据类型是指列.存储过程参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. 有一些数据是要存储为数字 ...

  5. mysql分组查询n条记录

    当业务逻辑越来越复杂时,这个查询的需求会越来越多,今天写成笔记记录下来,防止再忘记 SELECT * FROM `notice` a where add_time > 1513008000 an ...

  6. KVC基本使用

    首先,创建两个类.person类和book类.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/ ...

  7. 如何给UIViewController瘦身

    本文转载至  http://www.cocoachina.com/ios/20141128/10356.html 随着程序逻辑复杂度的提高,你是否也发现了App中一些ViewController的代码 ...

  8. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:bootstrap弹窗功能的实现

    在web前端的网页设计中,为了展示出简洁的网页风格和美观的效果,往往就会使用弹窗效果 在EasyNVR前端页面录像检索功能时,必然会播放录像,如果单独为播放录像文件排一个界面,用户在使用上会更加繁琐, ...

  9. mac sublime text 3 add ctags plugin

    https://www.smslit.top/2015/11/14/macSTctags-Develop/ ctags插件for sublime text项目和ctags源码项目都在github上.

  10. virtual dynamic shared object

    vdso(7) - Linux manual page http://man7.org/linux/man-pages/man7/vdso.7.html NAME | SYNOPSIS | DESCR ...