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. Atitit swt 4.3 4.4 4.5 新特性java attilax总结

    Atitit swt 4.3 4.4 4.5 新特性java attilax总结 1. 4.5 Release - June 3, 20151 1.1. Older Releases1 2. SWT  ...

  2. poj 2762 Going from u to v or from v to u?(强连通、缩点、拓扑)

    题意:(理解错了)在一个洞穴中有多个room,要求任意选两个room:u.v,都能保证u.v之间有通路,注意洞穴中的路是有向边.. 分析:强连通子图中的点必然两两之间可以互通,两个强连通子图之间有通路 ...

  3. js-音乐播放器,播放|暂停|滑块的功能

    音乐播放器,播放|暂停|滑块的功能 document.addEventListener('DOMContentLoaded', function loaded(event) { var audio = ...

  4. [译]GLUT教程 - 重整子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...

  5. 【cogs182】【USACO Jan07】均衡队形【st表】

    题目描写叙述 农夫约翰的 N (1 ≤ N ≤ 50,000) 头奶牛,每天挤奶时总会按相同的顺序站好. 一日.农夫约翰决定为奶牛们举行一个"终极飞盘"比赛.为简化问题.他将从奶牛 ...

  6. 【WPF学习笔记】之WPF基础:依赖关系属性和通知

    这些天来,对象似乎已经忙得晕头转向了.每个人都希望它们做这做那.Windows® Presentation Foundation (WPF) 应用程序中的典型对象会接到各种各样不同的请求:有要求绑定到 ...

  7. 模拟多级复选框效果--jquery

    今天又次体会到jquery的强大了,做了个多级复选框的效果,代码总共就20+行就over了. 我又想用js来做一个看看,才写了几个方法就写不动了,兼容性要考虑很多,而且代码量直线上升. 主要分享下jq ...

  8. CLR内存回收机制

    代龄机制. 通过递归构建可达对象图,不可达的对象会被回收,然后CLR会矫正对象指针. 对于终止化/Finalize对象, 一开始时这些对象指针/根/引用会被放到终止化链表中,当CLR垃圾收集开始时,那 ...

  9. vi相关命令

    vi相关命令在行首加#        :% s/^/#        删除#        :% s/#//在行末加#        :% s/$/#        删除        :% s/#$ ...

  10. 【Atheros】pktgen的ipv6地址处理函数参考及ipv6基本知识

    pktgen有很多函数可以作为很好的网络相关的工具函数,这里列出ipv6中1:0:0:0:0:0:0:1和1::1这两种地址形式相互转化的工具函数. 第一个函数,用于把一个1:0:0:0:0:0:0: ...