Operator Description
All 判断所有的元素是否满足条件
Any 判断存在一个元素满足条件
Contain 判断是否包含元素
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 = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; // checks whether all the students are teenagers
bool areAllStudentsTeenAger = studentList.All(s => s.Age > && s.Age < ); Console.WriteLine(areAllStudentsTeenAger);
bool isAnyStudentTeenAger = studentList.Any(s => s.age >  && s.age < );

Contains

public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value);

public static bool Contains<TSource>(this IEnumerable<TSource> source,
TSource value,
IEqualityComparer<TSource> comparer);
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 = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; Student std = new Student(){ StudentID =, StudentName = "Bill"};
bool result = studentList.Contains(std); //returns false

上面的result将是false,即使“”Bill"在集合中,因为Contains仅仅比较对象的引用,而不是对象的值。所以要比较对象的值,需要实现IEqualityComparer接口

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();
}
}
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 = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; Student std = new Student(){ StudentID =, StudentName = "Bill"};
bool result = studentList.Contains(std, new StudentComparer()); //returns true

LINQ 学习路程 -- 查询操作 Quantifier Operators All Any Contain的更多相关文章

  1. LINQ 学习路程 -- 查询操作 Conversion Operators

    Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...

  2. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  3. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  4. LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...

  5. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  6. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  7. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  8. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  9. LINQ 学习路程 -- 查询操作 Distinct Except Intersect Union

    Set Operators Usage Distinct 去掉集合的重复项 Except 返回两个集合的不同,第一个集合的元素不能出现在第二个集合中 Intersect 返回两个集合的交集,即元素同时 ...

随机推荐

  1. Android开发系列之系统源码目录

    相信大家对于Google给出的那副经典Android架构图非常的熟悉,从下往上依次是Linux内核层(主要是负责硬件管理调度),HAL层(主要是硬件抽象层),libs层+Runtime,Framewo ...

  2. webpack 使用配置

    注意:webpack 2.0版本之后有所区别 一.插件篇 1. 自动补全css3前缀 autoprefixer 官方是这样说的:Parse CSS and add vendor prefixes to ...

  3. 启动 ./spark-shell 命令报错

    当使用./spark-shell 命令报错 Caused by: ERROR XJ040: Failed to start database @476fde05, see the next excep ...

  4. Java集合系列之TreeMap源代码分析

    一.概述 TreeMap是基于红黑树实现的.因为TreeMap实现了java.util.sortMap接口,集合中的映射关系是具有一定顺序的,该映射依据其键的自然顺序进行排序或者依据创建映射时提供的C ...

  5. Web性能测试工具:http_load安装&使用简介

    除了siege,在Web性能测试工具中,http_load也是比较热门和常见的一款,有时因为种种原因,只能使用现成的工具,所以多了解和掌握一种Web性能测试工具是很有必要的. 1.下载安装包 略过 2 ...

  6. 第三篇:Logstash 安装配置

    Logstash 简介: Logstash 是一个实时数据收集引擎,可收集各类型数据并对其进行分析,过滤和归纳.按照自己条件分析过滤出符合数据导入到可视化界面.Logstash 建议使用java1.8 ...

  7. 【一键激活win8.1系统】

    下载激活工具地址: 链接:https://pan.baidu.com/s/1AUaQQRcqfCYWK94KapYjjA 密码:i4sk 下载后,右键单击“以管理员身份运行”Microsoft Too ...

  8. iPhone缓存网络数据

    本文转载至 http://blog.csdn.net/wwang196988/article/details/7542918   在iPhone应用程序中,我们经常要用去网络下载一些文件,比如xml, ...

  9. Office 365系列(-)

    昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...

  10. apche安装教程

    从Apache官网下载windows安装版的Apache服务器了, 现在分享给大家.   1 进入apache服务器官网http://httpd.apache.org/,这里我们以下载稳定版的 htt ...