LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到。

当你用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 延迟执行的更多相关文章
- LINQ 学习路程 -- 查询操作 GroupBy ToLookUp
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...
- LINQ 学习路程 -- 查询操作 Expression Tree
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...
- LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...
- LINQ 学习路程 -- 查询操作 Join
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...
- LINQ 学习路程 -- 查询操作 where
1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...
- LINQ 学习路程 -- 查询操作 let into关键字
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- LINQ 学习路程 -- 查询操作 Aggregate
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...
- LINQ 学习路程 -- 查询操作 Select, SelectMany
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...
- LINQ 学习路程 -- 查询操作 OfType
OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(); mixedList.Add(" ...
随机推荐
- maven依赖json-lib失败
© 版权声明:本文为博主原创文章,转载请注明出处 项目中需要使用到JSONArray,因此到将json-lib的依赖加入pom.xml中,但是一直下载失败 <dependency> < ...
- spark+kafka 小案例
(1)下载kafka的jar包 http://kafka.apache.org/downloads spark2.1 支持kafka0.8.2.1以上的jar,我是spark2.0.2,下载的kafk ...
- Unicode utf8等编码类型的原理
1.ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...
- getOutputStream() has already been called for this response的解决方法
1.问题描述:spring mvc中下载文件结束后,跳转到list页面,问题报上面的异常. 2.原因:写文件的时候response调用一次,在跳转的时候,spring调用ActionForward类中 ...
- delphi 无边框窗体常见问题
实现无边框窗体很简单,直接将窗体的BorderStyle属性设置为bsNone即可.但是这样会引起2个问题: 1.在xp系统下,任务栏鼠标右键点击无法弹出菜单 解决办法:在FormShow中加入这个过 ...
- cocos2dx游戏 地图
#include "HelloWorld.h" USING_NS_CC; CCScene* MyHelloWorld::scene() { // 'scene' is an aut ...
- 通过pyenv进行多版本python管理
1.安装pyenv brew install pyenv 2.配置.zshrc文件 export PYENV_ROOT=/usr/local/var/pyenv if which pyenv > ...
- 一、任天堂ns (Nintendo Switch) 上手
公司不方便回家详解做个博客非专业评测~
- iOS_3_图片浏览
终于效果图: BeyondViewController.h // // BeyondViewController.h // 03_图片浏览 // // Created by beyond on 14- ...
- thinkPHP5.0的添加(C操作)
首先创建表单: 后台表单用的是layui框架(模块化前端框架),有自己的表单验证,推荐大家使用,在这里表单我就不再贴代码了 其次后台处理: //接收数据并入库 $data = $this->re ...