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" }, , ...
随机推荐
- android性能优化学习笔记(加快应用程序启动速度:)
一:安卓中应用程序的启动方式有两种: 冷启动:后台没有该应用进程,系统会重新创建一个进程分配给该应用(所以会先创建和初始化Application类,再创建和初始化MainActivity,包括测量,布 ...
- IntelliJ IDEA 、genymotion模拟器、Android开发环境搭建
首先打开IDEA,看到该界面,如果没有该界面,请在User/用户名/IntelliJIDEAProjects/下删除所有项目文件夹.然后重启IDEA即可看到 接着开始配置jdk和sdk 然后在Proj ...
- angularjs2中的非父子组件的通信
AngualrJs2官方方法是以@Input,@Output来实现组件间的相互传值,而且组件之间必须父子关系,下面给大家提供一个简单的方法,实现组件间的传值,不仅仅是父子组件,跨模块的组件也可以实现传 ...
- iWatch开发:UI 组件说明
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hlbmppZTEyMzQ1Njc4/font/5a6L5L2T/fontsize/400/fill/I0 ...
- inline用于替代宏函数
在C&C++中 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionName(Va ...
- MongoDB可视化工具 Studio 3T
告别终端使用可视化工具Studio 3T对MongoDB进行数据库的操作. 简单的使用步骤介绍 1.启动MongoDB服务器(方法见MongoDB介绍与安装中的介绍) 2.连接MongoDB服务器 ...
- PLSQL使用技巧----加快你的编程效率
使用PLSQL 编程效率明显有所提高了 1.登录后默认自动选中My Objects 默认情况下,PLSQL Developer登录后,Brower里会选择All objects,如果你登录的 ...
- java的小知识点
1 获取当前路径 System.getProperty("user.dir") System.getProperty()参数大全# java.version ...
- PHP计算多少秒/分/时/天/周/月/年之前 : timeago
function timeago( $ptime ) { $etime = time() - $ptime; if ($etime < 59) return '刚刚'; $interval = ...
- HTML 与 SGML关系
HTML :超文本标记语言,“超文本”就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素 SGML:标准通用标记语言 HTML 是SGML下的一个应用