Transferring the understanding of a request to Enumerable extension methods to LINQ

How to transfer the following request to functional calls? I know that the compiler does this behind the scenes, but I don’t know how I will look at 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 };
+3
source share
1 answer

In this case, it is used SelectMany, and the concept is called transparent identifiers that preserve existing range variables. Thus, your request translates into:

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, it zis a transparent identifier. If there were an whereitem or something other than selectafter the last fromn, another transparent identifier would be entered.)

All translations are described in the C # language specification, section 7.16.

+6
source

Source: https://habr.com/ru/post/1777859/


All Articles