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

当你用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. CentOs yum源安装 nginx

    1 更新源 [root@server ~]#rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.no ...

  2. Xcode wifi连接真机调试

    设备环境:Mac OSX 10.12.5.iOS11.Xcode9 或以上版本 PS:这是WWDC2017的新功能,iOS11以上,Xcode9这是刚性要求.这个功能不好找,就记下来了 手机连接上Xc ...

  3. Weka关联规则分析

    购物篮分析: Apriori算法: 参数设置: 1.car 如果设为真,则会挖掘类关联规则而不是全局关联规则. 2. classindex 类属性索引.如果设置为-1,最后的属性被当做类属性. 3. ...

  4. 内核源码之Kconfig和Makefile

    转自:http://www.cnblogs.com/image-eye/archive/2011/08/28/2156005.html 内核源码之Kconfig和Makefile Linux内核源码树 ...

  5. Android Handler警告,SimpleDateFormat警告

    1:Handler// This Handler class should be static or leaks might occur: IncomingHandler    @SuppressLi ...

  6. C# 代码 手工 配置 Log4Net 2种方法

    这个初始化要在 获取 ILog 接口的代码之前完成, 之后按通常方式使用 log4net 就行了. 不用携带 config 配置文件. 方法1: /// <summary> /// 使用文 ...

  7. C语言基础知识【变量】

    C 变量1.变量其实只不过是程序可操作的存储区的名称.C 中每个变量都有特定的类型,类型决定了变量存储的大小和布局,该范围内的值都可以存储在内存中,运算符可应用于变量上.变量的名称可以由字母.数字和下 ...

  8. LNMP环境搭建(三:PHP)

    1.获取php源码 # cd /usr/local/src/ # wget http://cn2.php.net/get/php-7.0.15.tar.gz/from/this/mirror 2.解压 ...

  9. Computer Transformation(简单数学题+大数)

    H - Computer Transformation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  10. 在fc6上搭tftpd

    公司的开发环境依然停留在fc6上,,,,对..很旧,旧到想死. 我在没有进一步熟悉ubuntu的基础上,为了保持ABI一致. 只能依旧在FC6 上开发. 可是现在发现开发完成,我要在fc6上文件到wi ...