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中,一个重要的特性就是延迟加载,是指查询操作并不是在查询运算符定义的时候执行,而是在真正使用集合中的数据时才执行(如:在遍历集合时 ...
随机推荐
- win7上Android环境搭建以及调试
工欲善其事必先利其器,好记性不如烂笔头.要学习一门新的语言,首先必须得先搭环境,否则没法实践.如果之前按照网上的提示,搭建过环境,而且环境比较复杂的话,我相信隔很长一段时间后,就会忘记,到真正用的时候 ...
- kubernetes1.9中部署dashboard
在1.9k8s中 dashboard可以有两种访问方式 kubeconfig(HTTPS)和token(http) 2018-03-18 一.基于token的访问1.下载官方的dashboardwge ...
- MVC4不支持EF6解决方案 && Nuget控制台操作说明
问题背景:MVC4不支持EF6,所以要把EF6卸载然后安装EF5.只能降低版本EF5+MVC4或者EF6+MVC5; 这时候: Uninstall-Package EntityFramework -F ...
- C# 值类型与引用类型的异同
int,bool,decimal等为值类型 List,Stream等为引用类型 用等号设置一个值类型变量等于另一个变量时,会完成复制,之后这两个变量相互之间没有任何影响: 对引用使用等号时,这两个引用 ...
- Xamarin Forms中WebView的自适应高度
在Xamarin.Forms中,WebView如果嵌套在StackLayout和RelativeLayout中必须要设置HeightRequest和WidthRequest属性才会进行渲染.可是在实际 ...
- 3.21li = [1,'a','b',2,3,'a'] # li[1] = 'dfasdfas' # print(li)
一.增:li = [1,'a','b',2,3,'a']#按照索引去增加 li.insert(0,55) print(li) #增加到最后面 li.append('aaa') li.append([1 ...
- [SDOI2010] 魔法猪学院
Description 给定e和边权,求有多少条不同的道路能从1到n使得边权之和的和小于e Solution A*裸题 娘的要是SPFA再把dis写成to就剁手 // By YoungNeal #in ...
- 二分查找(binary search)java实现及时间复杂度
概述 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是拿low到high的正中间的值,我们假设是m,来跟v相比,如果m& ...
- Laravel 模型事件入门
Laravel 模型事件允许你监听模型生命周期内的多个关键点,甚至可以在阻止一个模型的保存或者删除. Laravel 模型事件文档 概述了如何使用钩子将对应事件与相关的事件类型关联起来,但是本文的主旨 ...
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...