Linq 等式运算符:SequenceEqual
检查元素的数量,每个元素的值及两个集合中元素的顺序是否相等,3个方面都相等则为true,否则为false
IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Three"};
IList<string> strList2 = new List<string>(){"One", "Two", "Three", "Four", "Three"};
bool isEqual = strList1.SequenceEqual(strList2); // returns true
IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Three"};
IList<string> strList2 = new List<string>(){ "Two", "One", "Three", "Four", "Three"};
bool isEqual = strList1.SequenceEqual(strList2); // returns false
//如果是引用类型,则比较的是引用
Student std = new Student() { StudentID = , StudentName = "Bill" };
IList<Student> studentList1 = new List<Student>(){ std };
IList<Student> studentList2 = new List<Student>(){ std };
bool isEqual = studentList1.SequenceEqual(studentList2); // returns true
Student std1 = new Student() { StudentID = , StudentName = "Bill" };
Student std2 = new Student() { StudentID = , StudentName = "Bill" };
IList<Student> studentList3 = new List<Student>(){ std1};
IList<Student> studentList4 = new List<Student>(){ std2 };
isEqual = studentList3.SequenceEqual(studentList4);// returns false
//在上面的示例中,studentList1和studentList2包含相同的学生对象std。 所以studentList1.SequenceEqual(studentList2)返回true。 但是,stdList1和stdList2包含两个独立的学生对象std1和std2。
//所以现在,stdList1.SequenceEqual(stdList2)将返回false.
//要比较复杂类型的两个集合的值,您需要实现IEqualityComperar <T>接口,如下所示
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.GetHashCode();
}
}
// following returns true
bool isEqual = studentList1.SequenceEqual(studentList2, new StudentComparer());
Linq 等式运算符:SequenceEqual的更多相关文章
- Linq 等式运算符:SequenceEqual(转载)
https://www.bbsmax.com/A/nAJvbKywJr/ 引用类型比较的是引用,需要自己实现IEqualityComparer 比较器. IList<string> str ...
- Linq 生成运算符 Empty,Range,Repeat
var c1 = Enumerable.Empty<string>();//c1.Count=0 , );//{9527,9528,9529,......9536} , );//{9527 ...
- Linq 连接运算符:Concat
//Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...
- Linq 连接运算符:Concat,Union
//Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...
- 第5章 LINQ
5.4 LINQ查询运算符 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- Linq/List/Array/IEnumerable等集合操作
来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1 LINQ查询结果集 1 2 Sy ...
- LINQ 方法
过滤操作符 Where 运算符(Linq扩展方法)根据给定条件过滤集合. 在其中扩展方法有以下两个重载.一个过载需要Func <TSource,bool>输入参数和第二个重载方法需要Fun ...
- c# linq查询语句详细使用介绍
本文介绍Linq的使用方法 linq介绍 LINQ只不过是实现IEnumerable和IQueryable接口的类的扩展方法的集合. LINQ可以查询IEnumerable集合或者IQueryable ...
- LINQ之延迟加载及其原理
这是LINQ(集成化查询)的继续及补充,在前面我已经介绍过,在LINQ中,一个重要的特性就是延迟加载,是指查询操作并不是在查询运算符定义的时候执行,而是在真正使用集合中的数据时才执行(如:在遍历集合时 ...
随机推荐
- Injection of autowired dependencies failed
error:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainCo ...
- Scala学习笔记(一)
scala 版HelloWorrld object HelloWorld{ def main(args:Array[String]){ println("Hello World!!!&quo ...
- 杭电oj-1000-A+B
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...
- ubuntu字符界面怎么设置中文显示和中文输入
在ubuntu的字符登陆界面,中文显示默认是乱码的,而且也不支持中文输入,解决方法有很多, 1)安装zhcon--解决中文显示乱码的问题. sudo apt-get install zhcon 然后c ...
- Asp.Net MVC 实现将Easy-UI展示数据下载为Excel 文件
在一个项目中,需要做一个将Easy-UI界面展示数据下载为Excel文件的功能,经过一段时间努力,完成了一个小Demo.界面如下: 但按下导出Excel后,Excel文件将会下载到本地,在office ...
- IE浏览器URL中文传参,后端接收是乱码问题处理
这个问题还是因为IE浏览器是国外产品,人家交流的主要语言是英语,中文不识别. 直接上代码,亲测无误. //判断是否是IE浏览器 function isIE() { var userAgent = na ...
- The POM for ... is missing, no dependency information available
今天在重温淘淘商城的项目,准备用idea重写次,换个bootstrap的前端框架,但是在用idea构建maven项目后编译时却报错了: 经再三确认,common工程自身并没有任何问题,引用这个工程的地 ...
- C语言描述链表的实现及操作
一.链表的创建操作 // 操作系统 win 8.1 // 编译环境 Visual Stuido 2017 #include<stdio.h> #include<malloc.h> ...
- 不安装oracle客户端连接oracle数据库
PLSQL Developer 或Toad 不安装Oracle 客户端连接数据库 为了简化Oracle在个人电脑的使用,避免占用不必要的资源,可以不安装Oracle客户端.方法是:使用Oracle I ...
- iPhone页面的常用调试方法
在iPhone中调试,大体上与上文 安卓中的移动页面调试 类似,区别主要是iOS系统中的一些限制,导致某些工具无法使用. 本文基于此,简要介绍在iPhone中如何调试页面. 最终可以实现在Mac平台使 ...