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(" ...
随机推荐
- 在caffe中用训练好的 caffemodel 来分类新的图片所遇到的问题
结合之前的博客: http://www.cnblogs.com/Allen-rg/p/5834551.html#3949333 用caffemodel去测试单通道的图像(mnist数据集)时,出现了问 ...
- eclipse下构建maven spring项目
准备工作: 1.下载eclipse(Eclipse Java EE IDE for Web Developers,Version: Juno Service Release 2). 2.下载maven ...
- systemctl使用说明
# systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...
- 很全的php数组操作方法
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- 利用freemarker生成带fusioncharts图片的word简报
/** * 利用freemarker生成带fusioncharts图片的word简报 * 烟台海颐软件技术论坛 * 作者 牟云飞 新建 * 毕业 ...
- vue实践---vue动态加载组件
开发中遇到要加载10个或者更多的,类型相同的组件时,如果用普通的 import 引入组件,components注册组件,代码显得太啰嗦了,这时候就需要用到 require.context 动态加载这些 ...
- Configure the modules to be find by modprobe
sudo ln -s /path/to/module.ko /lib/modules/`uname -r` sudo depmod -a #depmod will output a dependenc ...
- 一个手动备份MySQL数据库的脚本
#!/bin/bash username=root hostname=localhost password=root mysql -u$username -h$hostname -p$password ...
- js验证金额是否符合要求的正则表达式
正则的只是就不在这里重复的讲了,直接上代码 var mny = /^(((([1-9]([0-9]{0,8}))|0)\.([0-9]{1,2}))|([1-9]([0-9]{0,8})))$/; m ...
- java拾遗1----XML解析(一) DOM解析
XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...