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. Fiddler 过滤器的使用

    只显示制定HOST的SESSION

  2. php数组操作,内容相同,键值不同,互换

    $title = array("A"=>"创建时间","C"=>"商品信息","D"=& ...

  3. Nginx访问日志和错误日志的拆分(Logstash)

    >> from zhuhaiqing.info input { file { type =>> "nginx-access" path =>> ...

  4. oracle中的minus数据比对

    1.要有唯一索引或者主键作为前提,减少数据冲突的数量,如示例标红的地方:   2.当有in查询的时候,尽量用exists,这样能提高查询效率: create table TF_F_USER_DIFF1 ...

  5. member access within misaligned address 0x000000000031 for type 'struct ListNode', which requires 8 byte alignment

    在做LeetCode的two sum题目的时候,提交代码遇到了如题的runtime error,后来在一篇博客上看到了解决方法. 现有如下结构体: struct ListNode { int val; ...

  6. 关于inittab的几个命令

    1. 查看default runlevel(默认运行等级)的方法: $cat /etc/inittab | grep id id:3:initdefault: # <id>:<run ...

  7. SetTimer时间间隔的问题

    1.用WM_TIMER来设置定时器   SetTimer函数的原型 UINT_PTR SetTimer( HWND hWnd,                      // 窗体句柄 UINT_PT ...

  8. DB2 中like的通配符以及escape关键字定义转义字符的使用

    DB2 LIKE谓词查询语句中支持 百分号(%).下划线(_)的使用,不支持方括号([])(注:它会把方括号当成实际的值而非通配符),当我们需要在LIKE 查询条件中将百分号(%).下划线(_)作为实 ...

  9. yum 无法安装mysql

    昨晚帮盆友搭建服务器时,一直出现yum mysql 无法安装.报错信息如下: Transaction Check Error:  file /etc/my.cnf from install of my ...

  10. bat命令遍历文件和bat参数说明

    **************************************************************************************************** ...