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(" ...
随机推荐
- Codeforces 34C-Page Numbers(set+vector+暴力乱搞)
C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 编程算法 - 求1+2+...+n(构造函数) 代码(C++)
求1+2+...+n(构造函数) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whi ...
- Windows下UEFI环境的搭建
Windows下UEFI环境的搭建 一.环境准备 1. 安装2012及以上VS https://visualstudio.microsoft.com/ 2.下载NASM 2.13.03 http:/ ...
- 机器学习7—AdaBoost学习笔记
Adaboost算法原理分析和实例+代码(简明易懂)(转载) [尊重原创,转载请注明出处] http://blog.csdn.net/guyuealian/article/details/709953 ...
- 题目3 : Fibonacci
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given a sequence {an}, how many non-empty sub-sequence of it ...
- (2) yum源配置-163
1.获取yum源文件 登录http://mirrors.163.com/.help/centos.html,查看CentOS6的链接地址(右键点击“CentOS6”,选择复制链接地址),链接地址为:h ...
- CentOS 源码安装svn
一. 下载依赖包 1. apr源码包 http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.5.2.tar.gz 2. apr-util源码包 h ...
- 【WPF学习笔记】之依赖属性
概述: Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能.这些服务通常统称为 WPF 属性系统.由 ...
- 大数据hadoop之zookeeper
一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...
- 零基础学python-2.7 列表与元组
事实上,能够把列表和元组看成普通的数组.可是这个数组能够存储不同的数据类型(对象) 列表和元组的差别 列表 元组 使用的符号 [] () 元素数量 可变 不可变 改动元素 不能够 能够 假设大家有 ...