LINQ 学习路程 -- 查询操作 Distinct Except Intersect Union
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. |
- IList<string> strList = new List<string>(){ "One", "Two", "Three", "Two", "Three" };
- IList<int> intList = new List<int>(){ , , , , , , , };
- var distinctList1 = strList.Distinct();
- foreach(var str in distinctList1)
- Console.WriteLine(str);
- var distinctList2 = intList.Distinct();
- foreach(var i in distinctList2)
- Console.WriteLine(i);
如果要去掉复杂类型的重复项,需要实现IEqualityComparer接口
- public class Student
- {
- public int StudentID { get; set; }
- public string StudentName { get; set; }
- public int Age { get; set; }
- }
- class StudentComparer : IEqualityComparer<Student>
- {
- public bool Equals(Student x, Student y)
- {
- if (x.StudentID == y.StudentID
- && x.StudentName.ToLower() == y.StudentName.ToLower())
- return true;
- return false;
- }
- public int GetHashCode(Student obj)
- {
- return obj.StudentID.GetHashCode();
- }
- }
- IList<Student> studentList = new List<Student>() {
- new Student() { StudentID = , StudentName = "John", Age = } ,
- new Student() { StudentID = , StudentName = "Steve", Age = } ,
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Ron" , Age = }
- };
- var distinctStudents = studentList.Distinct(new StudentComparer());
- foreach(Student std in distinctStudents)
Except
第一个集合的元素不在第二个集合中出现,返回新的集合
- IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Five" };
- IList<string> strList2 = new List<string>(){"Four", "Five", "Six", "Seven", "Eight"};
- var result = strList1.Except(strList2);
- foreach(string str in result)
- Console.WriteLine(str);
- public class Student
- {
- public int StudentID { get; set; }
- public string StudentName { get; set; }
- public int Age { get; set; }
- }
- class StudentComparer : IEqualityComparer<Student>
- {
- public bool Equals(Student x, Student y)
- {
- if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
- return true;
- return false;
- }
- public int GetHashCode(Student obj)
- {
- return obj.StudentID.GetHashCode();
- }
- }
- public class Student
- {
- public int StudentID { get; set; }
- public string StudentName { get; set; }
- public int Age { get; set; }
- }
- class StudentComparer : IEqualityComparer<Student>
- {
- public bool Equals(Student x, Student y)
- {
- if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
- return true;
- return false;
- }
- public int GetHashCode(Student obj)
- {
- return obj.StudentID.GetHashCode();
- }
- }
Intersect
返回两个集合的交集
- IList<string> strList1 = new List<string>() { "One", "Two", "Three", "Four", "Five" };
- IList<string> strList2 = new List<string>() { "Four", "Five", "Six", "Seven", "Eight"};
- var result = strList1.Intersect(strList2);
- foreach(string str in result)
- Console.WriteLine(str);
复杂类型的交集需要实现IEqualityComparer<T>接口
- public class Student
- {
- public int StudentID { get; set; }
- public string StudentName { get; set; }
- public int Age { get; set; }
- }
- class StudentComparer : IEqualityComparer<Student>
- {
- public bool Equals(Student x, Student y)
- {
- if (x.StudentID == y.StudentID &&
- x.StudentName.ToLower() == y.StudentName.ToLower())
- return true;
- return false;
- }
- public int GetHashCode(Student obj)
- {
- return obj.StudentID.GetHashCode();
- }
- }
- IList<Student> studentList1 = new List<Student>() {
- new Student() { StudentID = , StudentName = "John", Age = } ,
- new Student() { StudentID = , StudentName = "Steve", Age = } ,
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Ron" , Age = }
- };
- IList<Student> studentList2 = new List<Student>() {
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Ron" , Age = }
- };
- var resultedCol = studentList1.Intersect(studentList2, new StudentComparer());
- foreach(Student std in resultedCol)
- Console.WriteLine(std.StudentName);
Union
两个集合的并集
- IList<string> strList1 = new List<string>() { "One", "Two", "three", "Four" };
- IList<string> strList2 = new List<string>() { "Two", "THREE", "Four", "Five" };
- var result = strList1.Union(strList2);
- foreach(string str in result)
- Console.WriteLine(str);
复杂类型的并集需要实现IEqualityComparer<T>接口
- public class Student
- {
- public int StudentID { get; set; }
- public string StudentName { get; set; }
- public int Age { get; set; }
- }
- class StudentComparer : IEqualityComparer<Student>
- {
- public bool Equals(Student x, Student y)
- {
- if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
- return true;
- return false;
- }
- public int GetHashCode(Student obj)
- {
- return obj.StudentID.GetHashCode();
- }
- }
- IList<Student> studentList1 = new List<Student>() {
- new Student() { StudentID = , StudentName = "John", Age = } ,
- new Student() { StudentID = , StudentName = "Steve", Age = } ,
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Ron" , Age = }
- };
- IList<Student> studentList2 = new List<Student>() {
- new Student() { StudentID = , StudentName = "Bill", Age = } ,
- new Student() { StudentID = , StudentName = "Ron" , Age = }
- };
- var resultedCol = studentList1.Union(studentList2, new StudentComparer());
- foreach(Student std in resultedCol)
- Console.WriteLine(std.StudentName);
LINQ 学习路程 -- 查询操作 Distinct Except Intersect Union的更多相关文章
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
随机推荐
- SpringSecurity---javaconfig:Hello Web Security
© 版权声明:本文为博主原创文章,转载请注明出处 本文根据官方文档加上自己的理解,仅供参考 官方文档:https://docs.spring.io/spring-security/site/docs/ ...
- java之静态代理
© 版权声明:本文为博主原创文章,转载请注明出处 定义: - 为其他对象提供一种代理以控制对这个对象的访问 组成: 抽象角色:通过接口或抽象类声明真正角色实现的业务方法 真实角色:实现抽象角色,定义真 ...
- JQuery小结(转)
一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...
- inotify+rsync
backup_to_rsync.sh #!/bin/bash #source function library . /etc/init.d/functions rsync_host=rsync.eti ...
- SPI协议介绍
一.概述 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控制 ...
- java多线程编码注意事项
Sole purpose of using concurrency is to produce scalable and faster program. But always remember, sp ...
- iOS CAGradientLayer白色渐变至上向下
项目需求当显示富文本内容高度太高的的时候不全部显示出来,而是显示查看更多按钮,当点击查看更多时把全部内容展开.同时未展开部分要加一个渐变模糊的效果. 上效果图: 这里要用到CAGradientLaye ...
- ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs
ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs 第一部分:使用ssh key 实现服务器间的免密码交互登陆 步骤1: 安装openssh-clients [root@001 ...
- ICloneable接口 Clone 深拷贝 浅拷贝
需要字段本身也实现了深拷贝Clone.应用场景不多,意义不大. 1. 隐含式地要求其子类和引用类也要实现ICloneable接口,如果引用层次比较深类似一个网状或树形接口,增加复杂性. 2. 考虑给s ...
- 信号量semaphore解析
1 基础概念 信号量在创建时须要设置一个初始值,表示同一时候能够有几个任务能够訪问该信号量保护的共享资源.初始值为1就变成相互排斥锁(Mutex),即同一时候仅仅能有一个任务能够訪问信号量保护的共享资 ...