Set Operators Usage
Distinct 去掉集合的重复项
Except 返回两个集合的不同,第一个集合的元素不能出现在第二个集合中
Intersect 返回两个集合的交集,即元素同时出现在两个集合中
Union Returns unique elements from two sequences, which means unique elements that appear in either of the two sequences.

  1. IList<string> strList = new List<string>(){ "One", "Two", "Three", "Two", "Three" };
  2.  
  3. IList<int> intList = new List<int>(){ , , , , , , , };
  4.  
  5. var distinctList1 = strList.Distinct();
  6.  
  7. foreach(var str in distinctList1)
  8. Console.WriteLine(str);
  9.  
  10. var distinctList2 = intList.Distinct();
  11.  
  12. foreach(var i in distinctList2)
  13. Console.WriteLine(i);

如果要去掉复杂类型的重复项,需要实现IEqualityComparer接口

  1. public class Student
  2. {
  3. public int StudentID { get; set; }
  4. public string StudentName { get; set; }
  5. public int Age { get; set; }
  6. }
  7.  
  8. class StudentComparer : IEqualityComparer<Student>
  9. {
  10. public bool Equals(Student x, Student y)
  11. {
  12. if (x.StudentID == y.StudentID
  13. && x.StudentName.ToLower() == y.StudentName.ToLower())
  14. return true;
  15.  
  16. return false;
  17. }
  18.  
  19. public int GetHashCode(Student obj)
  20. {
  21. return obj.StudentID.GetHashCode();
  22. }
  23. }
  1. IList<Student> studentList = new List<Student>() {
  2. new Student() { StudentID = , StudentName = "John", Age = } ,
  3. new Student() { StudentID = , StudentName = "Steve", Age = } ,
  4. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  5. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  6. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  7. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  8. new Student() { StudentID = , StudentName = "Ron" , Age = }
  9. };
  10.  
  11. var distinctStudents = studentList.Distinct(new StudentComparer());
  12.  
  13. foreach(Student std in distinctStudents)

Except

第一个集合的元素不在第二个集合中出现,返回新的集合

  1. IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Five" };
  2. IList<string> strList2 = new List<string>(){"Four", "Five", "Six", "Seven", "Eight"};
  3.  
  4. var result = strList1.Except(strList2);
  5.  
  6. foreach(string str in result)
  7. Console.WriteLine(str);
  1. public class Student
  2. {
  3. public int StudentID { get; set; }
  4. public string StudentName { get; set; }
  5. public int Age { get; set; }
  6. }
  7.  
  8. class StudentComparer : IEqualityComparer<Student>
  9. {
  10. public bool Equals(Student x, Student y)
  11. {
  12. if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
  13. return true;
  14.  
  15. return false;
  16. }
  17.  
  18. public int GetHashCode(Student obj)
  19. {
  20. return obj.StudentID.GetHashCode();
  21. }
  22. }
  1. public class Student
  2. {
  3. public int StudentID { get; set; }
  4. public string StudentName { get; set; }
  5. public int Age { get; set; }
  6. }
  7.  
  8. class StudentComparer : IEqualityComparer<Student>
  9. {
  10. public bool Equals(Student x, Student y)
  11. {
  12. if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
  13. return true;
  14.  
  15. return false;
  16. }
  17.  
  18. public int GetHashCode(Student obj)
  19. {
  20. return obj.StudentID.GetHashCode();
  21. }
  22. }

Intersect

返回两个集合的交集

  1. IList<string> strList1 = new List<string>() { "One", "Two", "Three", "Four", "Five" };
  2. IList<string> strList2 = new List<string>() { "Four", "Five", "Six", "Seven", "Eight"};
  3.  
  4. var result = strList1.Intersect(strList2);
  5.  
  6. foreach(string str in result)
  7. Console.WriteLine(str);

复杂类型的交集需要实现IEqualityComparer<T>接口

  1. public class Student
  2. {
  3. public int StudentID { get; set; }
  4. public string StudentName { get; set; }
  5. public int Age { get; set; }
  6. }
  7.  
  8. class StudentComparer : IEqualityComparer<Student>
  9. {
  10. public bool Equals(Student x, Student y)
  11. {
  12. if (x.StudentID == y.StudentID &&
  13. x.StudentName.ToLower() == y.StudentName.ToLower())
  14. return true;
  15.  
  16. return false;
  17. }
  18.  
  19. public int GetHashCode(Student obj)
  20. {
  21. return obj.StudentID.GetHashCode();
  22. }
  23. }
  1. IList<Student> studentList1 = new List<Student>() {
  2. new Student() { StudentID = , StudentName = "John", Age = } ,
  3. new Student() { StudentID = , StudentName = "Steve", Age = } ,
  4. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  5. new Student() { StudentID = , StudentName = "Ron" , Age = }
  6. };
  7.  
  8. IList<Student> studentList2 = new List<Student>() {
  9. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  10. new Student() { StudentID = , StudentName = "Ron" , Age = }
  11. };
  12.  
  13. var resultedCol = studentList1.Intersect(studentList2, new StudentComparer());
  14.  
  15. foreach(Student std in resultedCol)
  16. Console.WriteLine(std.StudentName);

Union

两个集合的并集

  1. IList<string> strList1 = new List<string>() { "One", "Two", "three", "Four" };
  2. IList<string> strList2 = new List<string>() { "Two", "THREE", "Four", "Five" };
  3.  
  4. var result = strList1.Union(strList2);
  5.  
  6. foreach(string str in result)
  7. Console.WriteLine(str);

复杂类型的并集需要实现IEqualityComparer<T>接口

  1. public class Student
  2. {
  3. public int StudentID { get; set; }
  4. public string StudentName { get; set; }
  5. public int Age { get; set; }
  6. }
  7.  
  8. class StudentComparer : IEqualityComparer<Student>
  9. {
  10. public bool Equals(Student x, Student y)
  11. {
  12. if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
  13. return true;
  14.  
  15. return false;
  16. }
  17.  
  18. public int GetHashCode(Student obj)
  19. {
  20. return obj.StudentID.GetHashCode();
  21. }
  22. }
  1. IList<Student> studentList1 = new List<Student>() {
  2. new Student() { StudentID = , StudentName = "John", Age = } ,
  3. new Student() { StudentID = , StudentName = "Steve", Age = } ,
  4. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  5. new Student() { StudentID = , StudentName = "Ron" , Age = }
  6. };
  7.  
  8. IList<Student> studentList2 = new List<Student>() {
  9. new Student() { StudentID = , StudentName = "Bill", Age = } ,
  10. new Student() { StudentID = , StudentName = "Ron" , Age = }
  11. };
  12.  
  13. var resultedCol = studentList1.Union(studentList2, new StudentComparer());
  14.  
  15. foreach(Student std in resultedCol)
  16. Console.WriteLine(std.StudentName);

LINQ 学习路程 -- 查询操作 Distinct Except Intersect Union的更多相关文章

  1. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  2. LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...

  3. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  4. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  5. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  6. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  7. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  8. LINQ 学习路程 -- 查询操作 Aggregate

    聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...

  9. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...

随机推荐

  1. SpringSecurity---javaconfig:Hello Web Security

    © 版权声明:本文为博主原创文章,转载请注明出处 本文根据官方文档加上自己的理解,仅供参考 官方文档:https://docs.spring.io/spring-security/site/docs/ ...

  2. java之静态代理

    © 版权声明:本文为博主原创文章,转载请注明出处 定义: - 为其他对象提供一种代理以控制对这个对象的访问 组成: 抽象角色:通过接口或抽象类声明真正角色实现的业务方法 真实角色:实现抽象角色,定义真 ...

  3. JQuery小结(转)

    一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...

  4. inotify+rsync

    backup_to_rsync.sh #!/bin/bash #source function library . /etc/init.d/functions rsync_host=rsync.eti ...

  5. SPI协议介绍

    一.概述 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控制 ...

  6. java多线程编码注意事项

    Sole purpose of using concurrency is to produce scalable and faster program. But always remember, sp ...

  7. iOS CAGradientLayer白色渐变至上向下

    项目需求当显示富文本内容高度太高的的时候不全部显示出来,而是显示查看更多按钮,当点击查看更多时把全部内容展开.同时未展开部分要加一个渐变模糊的效果. 上效果图: 这里要用到CAGradientLaye ...

  8. ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs

    ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs 第一部分:使用ssh key 实现服务器间的免密码交互登陆 步骤1: 安装openssh-clients [root@001 ...

  9. ICloneable接口 Clone 深拷贝 浅拷贝

    需要字段本身也实现了深拷贝Clone.应用场景不多,意义不大. 1. 隐含式地要求其子类和引用类也要实现ICloneable接口,如果引用层次比较深类似一个网状或树形接口,增加复杂性. 2. 考虑给s ...

  10. 信号量semaphore解析

    1 基础概念 信号量在创建时须要设置一个初始值,表示同一时候能够有几个任务能够訪问该信号量保护的共享资源.初始值为1就变成相互排斥锁(Mutex),即同一时候仅仅能有一个任务能够訪问信号量保护的共享资 ...