检查元素的数量,每个元素的值及两个集合中元素的顺序是否相等,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的更多相关文章

  1. Linq 等式运算符:SequenceEqual(转载)

    https://www.bbsmax.com/A/nAJvbKywJr/ 引用类型比较的是引用,需要自己实现IEqualityComparer 比较器. IList<string> str ...

  2. Linq 生成运算符 Empty,Range,Repeat

    var c1 = Enumerable.Empty<string>();//c1.Count=0 , );//{9527,9528,9529,......9536} , );//{9527 ...

  3. Linq 连接运算符:Concat

    //Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...

  4. Linq 连接运算符:Concat,Union

    //Concat()方法附加两个相同类型的序列,并返回一个新序列(集合)IList<string> strList = new List<string>() { "O ...

  5. 第5章 LINQ

    5.4 LINQ查询运算符 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  6. Linq/List/Array/IEnumerable等集合操作

    来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1    LINQ查询结果集    1 2    Sy ...

  7. LINQ 方法

    过滤操作符 Where 运算符(Linq扩展方法)根据给定条件过滤集合. 在其中扩展方法有以下两个重载.一个过载需要Func <TSource,bool>输入参数和第二个重载方法需要Fun ...

  8. c# linq查询语句详细使用介绍

    本文介绍Linq的使用方法 linq介绍 LINQ只不过是实现IEnumerable和IQueryable接口的类的扩展方法的集合. LINQ可以查询IEnumerable集合或者IQueryable ...

  9. LINQ之延迟加载及其原理

    这是LINQ(集成化查询)的继续及补充,在前面我已经介绍过,在LINQ中,一个重要的特性就是延迟加载,是指查询操作并不是在查询运算符定义的时候执行,而是在真正使用集合中的数据时才执行(如:在遍历集合时 ...

随机推荐

  1. js实现二叉树

    //binary tree//add order remove findfunction tree() { var node = function(key) {  this.left = null;  ...

  2. Windows Server 2019 预览版介绍

    在Windows server 2012.Windows server 2016还未完全普及的情况下,昨天Windows Server团队宣布Windows Server 2019将在2018年的下半 ...

  3. [Luogu 1395] 会议

    题目 Description 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会议,村长希望所有村民到会议地点的距离之和最小,那 ...

  4. 笔记:JDBC 数据库

    数据库 URL 在连接数据库时,我们必须使用各种与数据库类型相关的参数,例如主机名.端口号和数据库名称等,JDBC使用了一种与普通URL相类似的语法来描述数据库,JDBC URL 一般语法为: jdb ...

  5. PAT 个位数统计

    描述 给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个 ...

  6. Ubuntu 14.04下Hadoop2.4.1集群安装配置教程

    一.环境 系统: Ubuntu 14.04 64bit Hadoop版本: hadoop 2.4.1 (stable) JDK版本: OpenJDK 7 台作为Master,另3台作为Slave. 所 ...

  7. python中super()的一些用法

    在看python高级编程这本书的时候,在讲到super的时候,产生了一些疑惑,super在python中的用法跟其他的语言有一些不一样的地方,在网上找了一些资料,发现基本上很少有文章能把我的疑惑讲明白 ...

  8. 【网络】 数据链路层&物理层笔记

    数据链路层 简称链路层,功能在于将数据包封装成帧,控制帧对介质的访问.链路层连接了物理介质和其之上抽象层,可以使上层不用关心用什么方法在什么介质中传输信息. 在帧被传输时经过的每一跳,每个中间设备(通 ...

  9. 在用jQuery时遇到的小问题

    1. class类名问题? 在我在class ='看看(2)' ,凡是这样的居然给自身加其他style样式,居然添加不上,后来改成其他类名不带括号里的,居然好了. 2. line-height 引入的 ...

  10. linux --> fork()详解

    fork()详解 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个 ...