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. ObjC消息机制

    深入浅出ObjC之消息    罗朝辉(http://blog.csdn.net/kesalin) 在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC ...

  2. 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异

      导语:API Gateway是实现微服务重要的组件之一.面对诸多的开源API Gateway,如何进行选择也是架构师需要关注的焦点.本文作者对几个较大的开源API Gateway进行了压力测试,对 ...

  3. css 选择器之子元素

    /*html*/ <div class="wrap"> <span>1</span> <span>2</span> &l ...

  4. 【Caffe】caffemodel的大小计算(转载)

    看到一篇将如何计算caffemodel大小的blog,感觉对理解模型大小很有帮助. 原文地址:http://blog.csdn.net/u014696921/article/details/52413 ...

  5. xmlUtil 解析 创建

    http://yangzi09150915.blog.163.com/blog/static/32953487201072911410398/ package com.aibi.cmdc.webSer ...

  6. Markdown常用语法学习

    Markdown常用语法学习,这些就够用了. 演示地址: https://github.com/YalongYan/Markdown-- 特别提示:  标题'##'后面必须加一个空格,否则编译不对.# ...

  7. 将navigationbar的translucent属性设为No后,子控制器视图整体下移问题

    如果不将navigationbar.translucent = YES 会觉得颜色很浅,因为这是半透明状态 若navigationbar.translucent = NO,颜色问题解决,但是子控制器视 ...

  8. 执行cp命令时提示cp: 略过目录

    执行cp命令时提示cp: 略过目录 加入-r之后成功拷贝 在网上search了一下CP命令的用法: CP命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样, ...

  9. String、StringBuilder、 StringBuffer 深入分析 源代码解析

    java学习有一段时间了.但学习的东西都是框架等东西,java基础知识有点遗忘.所以重温一下java基础知识.写写文章里面有错的希望大家指正共同进步~~ 一.String 大家常常会说使用" ...

  10. Spring IOC(通过实例介绍,属性与构造方法注入)

    概括说明:下面通过实例介绍下属性方法注入.构造方法注入 1.源码结构图 2.代码介绍 (1).Dao接口 :UserDAO (2).Dao接口实现:UserDAOImpl (3).实体类:User ( ...