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的更多相关文章

  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. Spring事务管理之编程式事务管理

    © 版权声明:本文为博主原创文章,转载请注明出处 案例:利用Spring的编程式事务管理模拟转账过程 数据库准备 -- 创建表 CREATE TABLE `account`( `id` INT NOT ...

  2. 盘古分词demo,盘古分词怎么用

    1.下载PanGu.dll dll地址:http://download.csdn.net/detail/dhfekl/7493687 2.将PanGu.dll和词库引入到项目 最新词库地址:http: ...

  3. 阿里云 经典网络使用ClassicLink连接到专用网络后,192.168网段需要添加路由

    配置后,专用网络中的机器Ping不通经典网络, 查帮助说192.168网段需要手动添加路由表. 抓包能在经典网络中收到专用网络中过来的数据包,但回复的数据包不能正确发出去. 先用 route -n 查 ...

  4. asp.net core 系列之Response caching(1)

    这篇文章简单的讲解了response caching: 讲解了cache-control,及对其中的头和值的作用,及设置来控制response caching; 简单的罗列了其他的缓存技术:In-me ...

  5. 自定义type='file'上传文件样式

    改变默认的上传文件样式: 用label作为替代 <input id="file_-1" type="file" name="file" ...

  6. iOS 日志系统 本地日志打包上传到服务器

    日志系统主要包含两个部分 1.本地保存 我们知道NSLog打印的日志一般都是直接输出到控制台,开发人员可以在控制台直接看到实时打印的log,既然可以在控制台输出,那么能否将日志输出到其他地方呢,比如说 ...

  7. windows常用dos命令

    常用命令: d: 回车   磁盘切换 dir: 查看该目录下所有的文件和文件夹: md: 创建文件加 rd: 删除目录 cd: 进入指定的目录 cd..:回退到上级目录 cd\  :回退到根目录 de ...

  8. 浅谈<持续集成、持续交付、持续部署>(一)

    谈谈持续集成,持续交付,持续部署之间的区别 经常会听到持续集成,持续交付,持续部署,三者究竟是什么,有何联系和区别呢?   假如把开发工作流程分为以下几个阶段: 编码 -> 构建 -> 集 ...

  9. M - 基础DP

    M - 基础DP Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descriptio ...

  10. ORACLE client 11g r2 客户端开发环境配置

    一.安装ORACLE客户端,这里不做说明.需要注意的是,客户端解压位置应该在磁盘根目录下. 如果放在带中文字或者空格的文件名的路径下出了问题,可以放到磁盘根目录在安装.应该就会没有问题. 另外,一般安 ...