转载自诗人江湖老,原文地址

  C#在执行LINQ查询表达式的时候,并不会指定其执行语义,而是将查询表达式转换为遵循查询表达式模式的方法的调用。具体而言,查询表达式将转换为以下名称的调用:Where、Select、SelectMany、Join、GroupJoin、OrderBy、OrderByDescending、ThenBy、ThenByDescending、GroupBy、Cast等等。
  如同在前文中提到的用扩展方法和Lambda表达式简化LINQ查询表达式一般,这也是对查询表达式的一种转换。简化后的方法其实就是LINQ查询的实际执行。
  本文中用来示例的代码,参数设定都沿用上一篇文章的设定。

转换规则简述

  从查询表达式到方法调用的转换是一种句法映射,在执行任何类型绑定或重载决策之前发生。该转换可以保证在句法上正确,但不能保证生成语法正确的 C# 代码。转换查询表达式后,以常规方法调用的方式处理生成的方法调用,而这进而可能暴露错误,例如在方法不存在、参数类型错误或方法为泛型方法且类型推断失败这些情况下。
  不允许对查询表达式中的范围变量进行赋值。但允许 C# 实现在某些时候以不实施此限制,因为对于此处介绍的句法转换方案,有些时候可能根本无法实施此限制。
  某些转换使用由 * 指示的透明标识符注入范围变量。

转换规则讲解

带继续符的select和groupby子句的查询表达式的转换

  继续符是指 into 操作符,带有继续符的查询表达式类似如下:
   from ···into x ···
   转换为
   from x in (from ···) ···
   示例:

from c in customersgroup c by c.Country into g
select new { Country=c.Country , Customer = g.Count()}

   转换为

from g infrom c in customersgroup c by c.Country
select new { Country = g.Key, CustCount = g.Count() }

   最终转换为 

customers.
GroupBy(c => c.Country).
Select(g => new { Country = g.Key, CustCount = g.Count() })

含有显式范围变量类型的查询表达式的转换

   显式指定范围变量类型的 from 子句
   from T x in e
   转换为
   from x in (e).Cast<T>()

   显式指定范围变量类型的 join 子句
   join T x in e on k1 equals k2
   转换为
   join x in (e).Cast<T>()

   示例: 

from Customer in customers
where c.City == "London"
select c

   转换为

from c in customers.Cast<Customer>()
where c.City == "London"
select c

   最终转换为

customers.Cast<Customer>().Where(c=>c.City=="London")>

   显式范围变量类型对于查询实现非泛型 IEnumerable 接口的集合很有用,但对于实现泛型IEnumerable 接口的集合没什么用处。如果 customers 属于 ArrayList 类型,则在面的示例中即会如此。
  

退化查询表达式的转换

   退化查询表达式,是指选择源元素本身的查询,如:

from c in customers
select c

  确保查询表达式的结果永不为源对象本身非常重要,因为这样会向查询的客户端暴露源的类型和标识符。因此,在查询表达式为退化查询的时候,可通过在源上显式调用 Select 来保护直接以源代码形式写入的简并查询。然后,由 Select 实施者及其他查询操作员确保这些方法永远不会返回源对象本身。

  退化查询表达式如下:
  from x in e select x
  转换为
  (e).Select(x=>x)
  示例:

from c in customers
select c

  转换为

customers.Select(c => c)

from、 let、 where、 join 和 orderby 子句的转换

转换规则

  带有另一个 from 子句且后接一个 select 子句的查询表达式
  from x1 in e1
  from x2 in e2
  select v
  转换为
  (e1).SelectMany( x1 => e2 , ( x1 , x2 ) => v )

  带有另一个 from 子句且后接一个 select 子句的查询表达式
  from x1 in e1
  from x2 in e2
  ···
  转换为
  from * in (e1).SelectMany( x1 => e2 , ( x1 , x2 ) => new { x1 , x2 })

  带有 let 子句的查询表达式
  from x in e
  let y=f
  转换为
  from * in (e).Select( x => new { x , y = f })

  带有 where 子句的查询表达式
  from x in e
  where f
  ···
  转换为
  from x in (e).Where( x => f )

  带有 join 子句(不含 into)且后接 select 子句的查询表达式
  from x1 in e1
  join x2 in e2 on k1 equals k2
  select v
  转换为
  ( e1 ) . Join( e2 , x1 => k1 , x2 => k2 , ( x1 , x2 ) => v )

  带有 join 子句(不含 into)且后接除 select 子句之外的其他内容的查询表达式
  from x1 in e1
  join x2 in e2 on k1 equals k2
  
  转换为
  from * in ( e1 ) . Join(
  e2 , x1 => k1 , x2 => k2 , ( x1 , x2 ) => new { x1 , x2 })
  

  带有 join 子句(含 into)且后接 select 子句的查询表达式
  from x1 in e1
  join x2 in e2 on k1 equals k2 into g
  select v
  转换为
  ( e1 ) . GroupJoin( e2 , x1 => k1 , x2 => k2 , ( x1 , g ) => v )
  
  带有 join 子句(含 into)且后接除 select 子句之外的其他内容的查询表达式
  from x1 in e1
  join x2 in e2 on k1 equals k2 into g
  
  转换为
  from * in ( e1 ) . GroupJoin(
  e2 , x1 => k1 , x2 => k2 , ( x1 , g ) => new { x1 , g })
  

  带有 orderby 子句的查询表达式
  from x in e
  orderby k1 , k2 , … , kn
  
  转换为
  from x in ( e ) .
  OrderBy ( x => k1 ) .
  ThenBy ( x => k2 ).
  … .
  ThenBy ( x => kn )
  
  如果排序子句指定 descending 方向指示器,则将改为生成对 OrderByDescending 或
ThenByDescending 的调用。

转换规则实例演示

  我们假定,在下面的每个查询表达式中没有 let、 where、 join 或 orderby 子句,并且最多只有一个初始 from 子句。
  
  示例1:

  from c in customersfrom o in c.Ordersselect new { c.Name, o.OrderID, o.Total }

  转换为

customers.
SelectMany(c => c.Orders,(c,o) => new { c.Name, o.OrderID, o.Total })
  • 1
  • 2

  
  示例2:  

from c in customers
from o in c.Orders
orderby o.Total descending
select new { c.Name, o.OrderID, o.Total }

  转换为

from * in customers.SelectMany(c => c.Orders, (c,o) => new { c, o })
orderby o.Total descending
select new { c.Name, o.OrderID, o.Total }

  最终转换为

customers.SelectMany(c => c.Orders, (c,o) => new { c, o }).OrderByDescending(x => x.o.Total).Select(x => new { x.c.Name, x.o.OrderID, x.o.Total })

  其中 x 是编译器生成的以其他方式不可见且不可访问的标识符。

  
  示例3:

from o in orders
let t = o.Details.Sum(d => d.UnitPrice * d.Quantity)
where t >= 1000
select new { o.OrderID, Total = t }

  转换为

from * in orders.
Select(o => new { o, t = o.Details.Sum(d => d.UnitPrice * d.Quantity) })
where t >= 1000
select new { o.OrderID, Total = t }

  最终转换为

orders.Select(o => new { o, t = o.Details.Sum(d => d.UnitPrice * d.Quantity) }).Where(x => x.t >= 1000).Select(x => new { x.o.OrderID, Total = x.t })

  其中 x 是编译器生成的以其他方式不可见且不可访问的标识符。

  示例4:

from c in customers
join o in orders on c.CustomerID equals o.CustomerID
select new { c.Name, o.OrderDate, o.Total }

  转换为

customers.Join(orders, c => c.CustomerID, o => o.CustomerID,(c, o) => new { c.Name, o.OrderDate, o.Total })

  
  示例5:

from c in customers
join o in orders on c.CustomerID equals o.CustomerID into co
let n = co.Count()
where n >= 10
select new { c.Name, OrderCount = n }

  转换为

from * in customers.GroupJoin(orders, c => c.CustomerID, o => o.CustomerID,
(c, co) => new { c, co })
let n = co.Count()
where n >= 10
select new { c.Name, OrderCount = n }

  最终转换为

customers.GroupJoin(orders, c => c.CustomerID, o => o.CustomerID,(c, co) => new { c, co }).Select(x => new { x, n = x.co.Count() }).Where(y => y.n >= 10).Select(y => new { y.x.c.Name, OrderCount = y.n)

  其中 x 和 y 是编译器生成的以其他方式不可见且不可访问的标识符。
  
  示例6:

from o in orders
orderby o.Customer.Name, o.Total descending
select o

  转换为

orders.OrderBy(o => o.Customer.Name).ThenByDescending(o => o.Total)

select 子句的转换

  from x in e select v
  转换为
  ( e ) . Select ( x => v )
  当 v 为标识符 x 时,转换仅为
  ( e )
  

Groupby 子句的转换

  from x in e group v by k
  转换为
  ( e ) . GroupBy ( x => k , x => v )
  当 v 为标识符 x 时,转换为
  ( e ) . GroupBy ( x => k )

[深入学习C#]LINQ查询表达式详解(2)——查询表达式的转换相关推荐

  1. T-SQL查询进阶--详解公用表表达式(CTE)

    简介 对于SELECT查询语句来说,通常情况下,为了使T-SQL代码更加简洁和可读,在一个查询中引用另外的结果集都是通过视图而不是子查询来进行分解的.但是,视图是作为系统对象存在数据库中,那对于结果集 ...

  2. 【Java学习笔记之三十一】详解Java8 lambda表达式

    Java 8 发布日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Java 8之前 ...

  3. 前缀、中缀和后缀表达式详解,中缀表达式到后缀表达式的转换规则,以及后缀表达式的计算规则,附计算代码

    1. 中缀.前缀和后缀表达式 1.1 中缀表达式 首先,中缀表达式的这个"缀"指运算符在两个操作数的位置.中缀表达式其实就是我们常用的算术表达式,比如 2 + 9 - (32 * ...

  4. 【Java Lambda表达式】Lambda表达式详解、Lambda表达式的等效使用方式、多线程

    1.静态内部类 静态内部类,属于类中的类,好处是:如果不使用,就不会被编译. 如果把这个类放到方法中,成为局部内部类(看下一部分) package cn.hanquan.test;/*Lambda表达 ...

  5. cron表达式详解,cron表达式写法,cron表达式例子

    (cron = "* * * * * *") cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 例  "0 0 12 ...

  6. Quartz学习之Cron表达式详解

    Quartz学习之Cron表达式详解 一.cron表达式结构 **二.各字段的含义** 解释: 注意要点: 三.示例 一.cron表达式结构 cron表达式从左到右(用空格隔开):**秒 分 小时 月 ...

  7. java拉姆达表达式事例,Java Lambda表达式详解和实例

    简介 Lambda表达式是Java SE 8中一个重要的新特性.lambda表达式允许你通过表达式来代替功能接口. lambda表达式就和方法一样,它提供了一个正常的参数列表和一个使用这些参数的主体( ...

  8. java lambda表达式详解_Java8新特性Lambda表达式详解

    课程目标: 通过本课程的学习,详细掌握Java8新特性之Lambda表达式: 适用人群:有Java基础的开发人员: 课程概述:从Java 8出现以来lambda是最重要的特性之一,它可以让我们用简洁流 ...

  9. MySQL日志文件之错误日志和慢查询日志详解

    今天天气又开始变得很热了,虽然很热很浮躁,但是不能不学习,我在北京向各位问好.今天给大家分享一点关于数据库日志方面的东西,因为日志不仅讨厌而且还很重要,在开发中时常免不了与它的亲密接触,就在前几天公司 ...

最新文章

  1. android studio 怎么做屏幕适配?
  2. (19) 转载: 寻找丑数
  3. 浏览器开发调试工具的秘密 - Secrets of the Browser Developer Tools
  4. service mesh 数据平面nginmesh
  5. ASP.NET Core Kestrel 中使用 HTTPS (SSL)
  6. jzoj3347,bzoj3257-[NOI2013模拟]树的难题【树形dp】
  7. [html] 如何阻止屏幕旋转时自动调整字体的大小?
  8. python字典求平均值_Python 3.4 – 如何获得字典值的平均值?
  9. 倒计时 1 天!第十六届开源中国开源世界高峰论坛日程曝光,邀您共同缔造开源创新模式!...
  10. 只会写代码的程序员永远无法出色?!
  11. 解决laravel框架中Eloquent ORM的save方法无法插入数据的问题
  12. buck电路_Buck电路的多角度分析
  13. Select2使用经验及多Jquery版本处理
  14. Flume 的使用场景详解
  15. java获取手机通讯录权限_android获取手机通讯录
  16. 分数在c语言程序中怎么输入,如何在c语言中实现分数相加,以分数形式输出,并化为最简...
  17. C++迭代器之'插入迭代器'
  18. 密码学的骰子——随机数
  19. 软件测试工程师常见面试题
  20. Python将多张2D TIFF图片转为一个3D TIFF文件

热门文章

  1. MVVM架构~前台后台分离的思想与实践
  2. Redis学习手册(事务)
  3. 【软件领域知识整理】贵金属交易的名词解释(一)
  4. 移动端HTML5框架
  5. 解压zip,解决中文乱码
  6. CISICO 路由器和交换机的基本配置命令
  7. des加解密java c#,C#编写DES加密、解密类
  8. java注解_Java注解教程及自定义注解
  9. Caused by: Parent package is not defined: json-default - [unknown location]
  10. Linux ftp传送问题 WARNING! 258831 bare linefeeds received in ASCII mode