1.where

  

Filtering Operators Description
Where Returns values from the collection based on a predicate function
OfType Returns values from the collection based on a specified type. However, it will depend on their ability to cast to a specified type.
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate); public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
Func<TSource, int, bool> predicate);
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var filteredResult = from s in studentList
where s.Age > && s.Age <
select s.StudentName;

方式2

Func<Student,bool> isTeenAger = delegate(Student s) {
return s.Age > && s.Age < ;
}; var filteredResult = from s in studentList
where isTeenAger(s)
select s;

方式3

public static void Main()
{
var filteredResult = from s in studentList
where isTeenAger(s)
select s;
} public static bool IsTeenAger(Student stud)
{
return stud.Age > && stud.Age < ;
}

where的第二个扩展方法包含集合的index索引

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 = }
}; var filteredResult = studentList.Where((s, i) => {
if(i % == ) // if it is even element
return true; return false;
}); foreach (var std in filteredResult)
Console.WriteLine(std.StudentName);

多个where从句

var filteredResult = from s in studentList
where s.Age >
where s.Age <
select s;
var filteredResult = studentList.Where(s => s.Age > ).Where(s => s.Age < );

需要记住的几点:

1.Where根据特定条件来筛选集合元素

2.where扩展方法有2个重载,使用第二个重载方法可以知道当前元素在集合中的索引位置

3.方法语法需要整个lambda表达式,而查询语法仅仅需要表达式主体

4.在单一的LINQ查询中可以使用多个where从句

LINQ 学习路程 -- 查询操作 where的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. LINQ 学习路程 -- 查询操作 Aggregate

    聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...

  8. LINQ 学习路程 -- 查询操作 Select, SelectMany

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

  9. LINQ 学习路程 -- 查询操作 OfType

    OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(); mixedList.Add(" ...

随机推荐

  1. uboot和内核分区的改动

    随着内核的更新,内核越来越大,uboot给nand的kernel分区默认是2M的 device nand0 <nandflash0>, # parts = 4  #: name       ...

  2. 实现Nullable 可空类型

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace demo ...

  3. NOJ1167 丑陋数 想法题

    题意 丑陋数n的意思是n的全部素数因子仅仅有2,3,5. 求出前1500个丑陋数. (第一个丑陋数是1) 思路 用一个数组维护全部的丑陋数. 一開始数组中仅仅有一个数就是1. 如今能够确定的丑陋数还有 ...

  4. 通过Lock对象以及Condition对象实现多线程同步

    通过Lock对象以及Condition对象实现多线程同步: 在之前的学习中,无论是通过synchronized建立同步代码块,还是通过synchronized建立同步函数,都是把对象看成一把锁来实现同 ...

  5. 生成JNI的DLL时提示找不到jni.h的解决的方法Cannot open include file: &#39;jni.h&#39;: No such file or directory

    解决的方法: 就是到jdk的安装文件夹下include下把下面对应的文件,拷贝到vc文件夹下的include文件夹下 \jdk\include\jni.h \jdk\include\win32\jaw ...

  6. ipmitool常用命令

    然后查看ip等信息,使用如下命令: ipmitool lan print 远程访问 可使用如下命令尝试: ipmitool -I lanplus -H 10.108.125.227 -U Admini ...

  7. 网络流合集:bzoj1433,1934,1854 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23992205 网络流/二分图大合集 [NO.1*原题] 1433: [ZJOI200 ...

  8. 谷歌高管无意中透露Google Glass未获得成功的原因

    Google X高管Astro Teller在接受媒体采访时无意中透露了这款设备没有取得预期成绩的原因 最终我们发现,在他们生活的世界里,数字生活和即时物理生活根本无法融为一体. Teller提出的这 ...

  9. python函数-------python2.7教程学习【廖雪峰版】(三)

    任务: 看完函数这一章    已完成 2017年6月8日16:23:491.函数的作用:写较少的代码实现较多的功能,可以多次被调用.2.可见,借助抽象,我们才能不关心底层的具体计算过程,而直接在更高的 ...

  10. POJ1850&&POJ1496

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9236   Accepted: 4405 Description ...