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中,一个重要的特性就是延迟加载,是指查询操作并不是在查询运算符定义的时候执行,而是在真正使用集合中的数据时才执行(如:在遍历集合时 ...
随机推荐
- JWT 在前后端分离中的应用与实践
关于前后端分离 前后端分离是一个很有趣的议题,它不仅仅是指前后端工程师之间的相互独立的合作分工方式,更是前后端之间开发模式与交互模式的模块化.解耦化.计算机世界的经验告诉我们,对于复杂的事物,模块化总 ...
- jsp常用的jstl语法
<c:forEach items="<object>" begin="<int>" end="<int>&q ...
- 负载均衡,会话保持,session同步(转)
转自:http://bbs.linuxtone.org/thread-18212-1-1.html 一,什么负载均衡一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但 ...
- 《深入理解Bootstrap》读书笔记(二)
列表 1.普通列表 普通列表的使用没有什么变化,只是在原本的基础上对margin和行间距做了一些调整. 2.有序列表 有序列表的使用也是没有什么变化,只是在原本的基础上对margin和行间距做了一些调 ...
- python编程中的if __name__ == 'main与windows中使用多进程
if __name__ == 'main 一个python的文件有两种使用的方法,第一是直接作为程序执行,第二是import到其他的python程序中被调用(模块重用)执行. 因此if __name_ ...
- poj-1012-约瑟夫问题
Description The Joseph's problem is notoriously known. For those who are not familiar with the origi ...
- Spring中Bean的生命周期讨论
1.代码和配置文件准备 1.1.测试类(user.java) public class User { private int id; private String name; private int ...
- Spring事务管理—aop:pointcut expression解析(转)
本文转载自: http://hane00.blog.163.com/blog/static/160061522011427473965/ 先来看看这个spring的配置文件的配置: <!-- 事 ...
- JAVA代码提示
经过以上过程,整个项目需要的环境差不多搭建完成了.接下来一个小技巧,如下图进行配置之后就可以将只在.出现时进行代码提示换成任意字母+.出现时的代码提示了(.abcdefghijklmnopqrstuv ...
- Struts2 之 Action 类访问 WEB 资源
接着上次博客的内容我继续分享我所学到的知识,和自己在学习过程中所遇到问题以及解决方案.当然,如果读者发现任何问题均可以在下方评论告知我,先谢! 在 Action 中访问 WEB 资源 web 资源 所 ...