简体   繁体   English

将查询理解翻译为LINQ中的Enumerable扩展方法

[英]Translating query comprehension to Enumerable extension methods in LINQ

How do I translate the following query to functional calls? 如何将以下查询转换为函数调用? I know the compiler does this behind the scenes but don't know how I would view the result 我知道编译器在幕后执行此操作但不知道如何查看结果

        var query = from item in Enumerable.Range(0, 10)
                    from item2 in Enumerable.Range(item, 10)
                    from item3 in Enumerable.Range(item2, 10)
                    select new { item, item2, item3 };

In this case, it uses SelectMany , and a concept called transparent identifiers which preserve the existing range variables. 在这种情况下,它使用SelectMany和一个名为透明标识符的概念来保​​留现有的范围变量。 So your query would translate to: 因此,您的查询将转换为:

var query = Enumerable.Range(0, 10)
                      .SelectMany(item => Enumerable.Range(item, 10),
                                  (item, item2) => new { item, item2 })
                      .SelectMany(z => Enumerable.Range(z.item2, 10),
                                  (z, item3) => new { z.item, z.item2, item3 });

(In this case z is the transparent identifier. If there'd been a where clause or anything other than a select after the last from clause, another transparent identifier would have been introduced.) (在这种情况下, z是透明标识符。如果在最后一个from子句之后有where子句或者除了select之外的任何东西,则会引入另一个透明标识符。)

The translations are all described in the C# language specification, section 7.16. 翻译都在C#语言规范的第7.16节中描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM