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. BGP双线的真真假假

    BGP双线的真真假假: 国内不少IDC服务商都号称自己是“真正的双线”.“双线单IP”.“全路由双线”,但是,这其中有没有水分?他们都是BGP双线?BGP的门槛真的这么低吗? 首先,要构建真正的BGP ...

  2. angularjs2中的非父子组件的通信

    AngualrJs2官方方法是以@Input,@Output来实现组件间的相互传值,而且组件之间必须父子关系,下面给大家提供一个简单的方法,实现组件间的传值,不仅仅是父子组件,跨模块的组件也可以实现传 ...

  3. MySQL_使用时遇到的问题汇总

    一.data too long for column 'name' at row 1 1.现象:把数据库的字符集编码设置为utf-8,通过DOS界面向表的某一列插入汉字时会遇到类似 data too ...

  4. PHP中常见的几种运行代码的方式

    常见的运行程序的方法有 shell_exec ``(反引号) eval system exec passthru 下面分别介绍他们的用法: 名称 解释 返回值 注意 shell_exec 通过 she ...

  5. nginx大量TIME_WAIT的解决办法 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

    vi /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse=1 #让TIME_WAIT状态可以重用,这样即使TIME_W ...

  6. Pycharm context menu disable RUN option

    这个问题很坑.正常来说一个文件右键出来的是 Run 选项, 可是近期几个文件都是 Unititest 的測试选项,每次要执行的时候都要手工去配置Run Option,在尝试了: 0. 重置IDE配置 ...

  7. stm32DMA通道 ADC通道

    DMA: 1.使用DAC的时候.将转化后得到的模拟信号通过IO口输出的时候.为什么还将IO口配置能输入模式 PS:stm32手冊上定义PA4和PA5分别和DAC1通道和DAC2通道相连  : DMA1 ...

  8. 笔试真题解析 ALBB-2015 算法project师实习生机试

    1.用十进制计算30!(30的阶乘),将结果转换成3进制进行表示的话,该进制下的结果末尾会有____个0. [解析] 计算N.下3进制结果末尾有多少个0,事实上就是计算3进制中的3被进位多少次,仅仅要 ...

  9. Python装饰器 计时器记录方法执行性能

    import time def timeit(func): def wrapper(): start = time.clock() func() end =time.clock() print 'us ...

  10. eclipse tasks

    tasks可以在代码里增加标识,通过tasks view可以快速的找到这些标识的地方,有助于提高开发效率和代码管理. 通过Eclipse的 Window==>Show View==>Tas ...