对于 LINQ to Objects,这被编译为一组静态方法调用:
var productNames =
from p in products
where p.Id > 100 and p.Id < 5000
select p.ProductName;
Becomes:
IEnumerable<string> productNames = products
.Where(p => p.Id > 100 and p.Id < 5000)
.Select(p => p.ProductName);
这使用了中定义的扩展方法Enumerable类型,所以实际上编译为:
IEnumerable<string> productNames =
Enumerable.Select(
Enumerable.Where(products, p => p.Id > 100 and p.Id < 5000),
p => p.ProductName
);
处理此问题的 lambda 表达式由编译器转换为方法。 where 中的 lambda 变成了一个可以设置为 a 的方法Func<Product, Boolean>
,然后选择进入Func<Product, String>
.
有关详细说明,请参阅Jon Skeet 的博客系列:重新实现 LINQ to Objects。他详细介绍了其工作原理的整个过程,包括编译器转换(从查询语法到方法调用)、方法的实现方式等。
请注意,LINQ to Sql 和IQueryable<T>
实现方式不同。这Expression<T>
由 lambda 生成的数据被传递到查询提供程序,查询提供程序又以某种方式(由提供程序决定如何执行此操作)“转换”为调用,通常在 ORM 的情况下在服务器上运行。
对于该方法,例如:
private static IEnumerable<string> ProductNames(IEnumerable<Product> products)
{
var productNames =
from p in products
where p.Id > 100 && p.Id < 5000
select p.ProductName;
return productNames;
}
编译为以下 IL:
.method private hidebysig static class [mscorlib]System.Collections.Generic.IEnumerable`1<string> ProductNames(class [mscorlib]System.Collections.Generic.IEnumerable`1<class ConsoleApplication3.Product> products) cil managed
{
.maxstack 3
.locals init (
[0] class [mscorlib]System.Collections.Generic.IEnumerable`1<string> enumerable,
[1] class [mscorlib]System.Collections.Generic.IEnumerable`1<string> enumerable2)
L_0000: nop
L_0001: ldarg.0
L_0002: ldsfld class [mscorlib]System.Func`2<class ConsoleApplication3.Product, bool> ConsoleApplication3.Program::CS$<>9__CachedAnonymousMethodDelegate3
L_0007: dup
L_0008: brtrue.s L_001d
L_000a: pop
L_000b: ldnull
L_000c: ldftn bool ConsoleApplication3.Program::<ProductNames>b__2(class ConsoleApplication3.Product)
L_0012: newobj instance void [mscorlib]System.Func`2<class ConsoleApplication3.Product, bool>::.ctor(object, native int)
L_0017: dup
L_0018: stsfld class [mscorlib]System.Func`2<class ConsoleApplication3.Product, bool> ConsoleApplication3.Program::CS$<>9__CachedAnonymousMethodDelegate3
L_001d: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> [System.Core]System.Linq.Enumerable::Where<class ConsoleApplication3.Product>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>, class [mscorlib]System.Func`2<!!0, bool>)
L_0022: ldsfld class [mscorlib]System.Func`2<class ConsoleApplication3.Product, string> ConsoleApplication3.Program::CS$<>9__CachedAnonymousMethodDelegate5
L_0027: dup
L_0028: brtrue.s L_003d
L_002a: pop
L_002b: ldnull
L_002c: ldftn string ConsoleApplication3.Program::<ProductNames>b__4(class ConsoleApplication3.Product)
L_0032: newobj instance void [mscorlib]System.Func`2<class ConsoleApplication3.Product, string>::.ctor(object, native int)
L_0037: dup
L_0038: stsfld class [mscorlib]System.Func`2<class ConsoleApplication3.Product, string> ConsoleApplication3.Program::CS$<>9__CachedAnonymousMethodDelegate5
L_003d: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!1> [System.Core]System.Linq.Enumerable::Select<class ConsoleApplication3.Product, string>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>, class [mscorlib]System.Func`2<!!0, !!1>)
L_0042: stloc.0
L_0043: ldloc.0
L_0044: stloc.1
L_0045: br.s L_0047
L_0047: ldloc.1
L_0048: ret
}
注意这些都是正常的call
方法调用的说明。 lambda 被转换为其他方法,例如:
[CompilerGenerated]
private static bool <ProductNames>b__2(Product p)
{
return ((p.Id > 100) && (p.Id < 0x1388));
}