博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
101 LINQ Samples: Query Execution
阅读量:4341 次
发布时间:2019-06-07

本文共 2321 字,大约阅读时间需要 7 分钟。

Deferred Execution

The following sample shows how query execution is deferred until the query is enumerated at a foreach statement.

 
public void Linq99(){    // Sequence operators form first-class queries that    // are not executed until you enumerate over them.     int[] numbers = new int[] { 5413986720 };     int i = 0;    var q =        from n in numbers        select ++i;     // Note, the local variable 'i' is not incremented    // until each element is evaluated (as a side-effect):    foreach (var v in q)    {        Console.WriteLine("v = {0}, i = {1}", v, i);    }}

Result

v = 1, i = 1

v = 2, i = 2
v = 3, i = 3
v = 4, i = 4
v = 5, i = 5
v = 6, i = 6
v = 7, i = 7
v = 8, i = 8
v = 9, i = 9
v = 10, i = 10

Immediate Execution

The following sample shows how queries can be executed immediately with operators such as ToList().

 
public void Linq100(){    // Methods like ToList() cause the query to be    // executed immediately, caching the results.     int[] numbers = new int[] { 5413986720 };     int i = 0;    var q = (        from n in numbers        select ++i)        .ToList();     // The local variable i has already been fully    // incremented before we iterate the results:    foreach (var v in q)    {        Console.WriteLine("v = {0}, i = {1}", v, i);    }}

Result

v = 1, i = 10

v = 2, i = 10
v = 3, i = 10
v = 4, i = 10
v = 5, i = 10
v = 6, i = 10
v = 7, i = 10
v = 8, i = 10
v = 9, i = 10
v = 10, i = 10

Query Reuse

The following sample shows how, because of deferred execution, queries can be used again after data changes and will then operate on the new data.

 
public void Linq101(){    // Deferred execution lets us define a query once    // and then reuse it later after data changes.     int[] numbers = new int[] { 5413986720 };    var lowNumbers =        from n in numbers        where n <= 3        select n;     Console.WriteLine("First run numbers <= 3:");    foreach (int n in lowNumbers)    {        Console.WriteLine(n);    }     for (int i = 0; i < 10; i++)    {        numbers[i] = -numbers[i];    }     // During this second run, the same query object,    // lowNumbers, will be iterating over the new state    // of numbers[], producing different results:    Console.WriteLine("Second run numbers <= 3:");    foreach (int n in lowNumbers)    {        Console.WriteLine(n);    }}

Result

First run numbers <= 3:

1
3
2
0
Second run numbers <= 3:
-5
-4
-1
-3
-9
-8
-6
-7
-2
0

转载于:https://www.cnblogs.com/zjz008/archive/2011/03/13/1982979.html

你可能感兴趣的文章
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>