延迟执行是指一个表达式的值延迟获取,知道它的值真正用到。

当你用foreach循环时,表达式才真正的执行。

延迟执行有个最重要的好处:它总是给你最新的数据

实现延迟运行

你可以使用yield关键字实现延迟加载

public static class EnumerableExtensionMethods
{
public static IEnumerable<Student> GetTeenAgerStudents(this IEnumerable<Student> source)
{ foreach (Student std in source)
{
Console.WriteLine("Accessing student {0}", std.StudentName); if (std.age > && std.age < )
yield return std;
}
}
}
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 teenAgerStudents = from s in studentList.GetTeenAgerStudents()
select s; foreach (Student teenStudent in teenAgerStudents)
Console.WriteLine("Student Name: {0}", teenStudent.StudentName);

从上面输出的结果看出:当你用foreach循环遍历时,GetTeenAgerStudents()方法才会被调用

LINQ查询的立即执行

立即执行和延迟执行相反,它迫使LINQ查询立即执行并返回结果,

IList<Student> teenAgerStudents =
studentList.Where(s => s.age > && s.age < ).ToList();
IList<Student> teenAgerStudents = (from s in studentList
where s.age > && s.age <
select s).ToList();

LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行的更多相关文章

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

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

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

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

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

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

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

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

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

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  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. 电路板上为何要有孔洞?何谓PTH/NPTH/vias(导通孔)

      推荐文章:PCBA大讲堂:用数据比较OSP及ENIG表面处理电路板的焊接强度 如果你有机会拿起一片电路板,稍微观察一下会发现这电路板上有着许多大大小小的孔洞,把它拿起来对着天花板上的电灯看,还会发 ...

  2. 在CentOS 5下安装中文五笔

    由于习惯使用五笔,需要在CentOS5 下安装中文五笔输入法. 刚装好的 CentOS 5默认是没有中文输入 法的.只能显示英文,有中文字符的文件名呈现乱码. 首先挂载CentOS的系统安装盘,在安装 ...

  3. C#:异步编程和线程的使用(.NET 4.5 ),异步方法改为同步执行

    摘自:http://www.codeproject.com/Articles/996857/Asynchronous-programming-and-Threading-in-Csharp-N(葡萄城 ...

  4. ubuntu16.04 安装系统之后的开发必备-sourcelist--idk-sublime--opencv

    设置sourcelist.txt # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ub ...

  5. iOS ZipArchive文件解压缩

    ZipArchive可以用于iOS中文件的解压缩 压缩文件的方法: //将工程中picture添加到左面111.zip压缩文件中 如果崩溃请更换压缩路径 -(void)testZipFile{ //压 ...

  6. iOS iPhoneX/iPhoneXS/iPhoneXR/iPhoneXS Max系列适配

    以前异性屏只有一款iPhoneX,所以在适配的时候直接判断高度是否等于812即可判断是否是iPhoneX #define IS_IPHONE_X (IS_IPHONE && SCREE ...

  7. NOJ1167 丑陋数 想法题

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

  8. BZOJ 2176 Strange string 最小表示法

    题目大意:给定一个串S,求最小表示法 n<=1000W,实在不敢写后缀自己主动机,就去学了最小表示法= = 记得用unsigned char不然WA= = 数据真是逗- - #include & ...

  9. jquery简洁遮罩插件

    /************************** *Desc:提交操作时遮罩 *Argument:type=0 全屏遮 1局部遮 *Author:Zery-Zhang *Date:2014-09 ...

  10. JSP具体篇——application

    application对象 application对象用于保存全部应用程序中的共同拥有数据.它在server启动时自己主动创建.在server停止时自己主动销毁. 当application对象没有被销 ...