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. PLSQL中显示Cursor、隐示Cursor、动态Ref Cursor差别

    一.显式cursor 显式是相对与隐式cursor而言的,就是有一个明白的声明的cursor.显式游标的声明类似例如以下(具体的语法參加plsql ref doc ): cursor cursor_n ...

  2. Ubuntu 16.04.5下FFmpeg编译与开发环境搭建

    PC环境: Ubuntu 18.04 上面只要安装下面的提示安装即可,基本上不必再下载依赖库的源代码进行编译和安装 编译步骤: 1, 安装相关工具: sudo apt  install -y auto ...

  3. JLink defective

    下载了最新的JLink V622g,打开JLink命令行后,提示以下信息 The connected J-Link is defective,Proper operation cannot be gu ...

  4. Android Java 线程池 ThreadPoolExecutor源代码篇

    线程池简单点就是任务队列+线程组成的. 接下来我们来简单的了解下ThreadPoolExecutor的源代码. 先看ThreadPoolExecutor的简单类图,对ThreadPoolExecuto ...

  5. Android Studio gradle 文件中 ${supportLibVersion} 用法

    一般我们在项目中的gradle会添加如下库文件 dependencies { compile 'com.android.support:appcompat-v7:23.1.0' compile 'co ...

  6. Shiro 认证失败返回JSON

    Shiro框架默认认证失败后会返回到登录页面,在前后端分离项目中,需要返回JSON数据,以便前端或者app端解析处理. 实现方式: 1. 扩展shiro框架的UserFilter类,重写redirec ...

  7. SVN分支与主干

    我的理解:在svn版本库中创建两个目录,一个主干如truck,一个分支目录如branch(注:分支可以创建多个),分别在客户端中检出代码,在分支中进行bug的修复以及新模块的开发,开发完后再merge ...

  8. CSS3 - 鼠标移入移出时改变样式

    1,使用伪类实现样式切换伪类是CSS2.1时出现的新特性,让许多原本需要JavaScript才能做出来的效果使用CSS就能实现.比如实现下面的鼠标悬停效果,只要为:hover伪类应用一组新样式即可.当 ...

  9. python 正則表達式推断邮箱格式是否正确

    import re def validateEmail(email):     if len(email) > 7:         if re.match("^.+\\@(\\[?) ...

  10. python 基础 9.5 数据库连接池

      一. 数据库连接池    python 编程中可以使用MySQLdb 进行数据库的连接及诸如查询,插入,更新等操作,但是每次连接mysql 数据库请求时,都是独立的去请求访问,相当浪费资源,而且访 ...