If you squint hard enough, you can almost believe that C# 7 now has anonymous iterators that you can use smack in the middle of your LINQ comprehension to introduce some ah-hoc operator:
from seq in new[] {
new Func<int, int, int, IEnumerable<int>>((start, stop, step) => { return Seq(); IEnumerable<int> Seq() {
for (var i = start; i < stop; i += step)
yield return i;
}})
}
from x in seq(0, 3, 1)
from y in seq(x, x + 5, 2)
select new { x, y }
Try it in C# Interactive 2.0 to see it in action:
Microsoft (R) Visual C# Interactive Compiler version 2.0.0.61501
Copyright (C) Microsoft Corporation. All rights reserved.
Type "#help" for more information.
> from seq in new[] {
. new Func<int, int, int, IEnumerable<int>>((start, stop, step) => { return Seq(); IEnumerable<int> Seq() {
. for (var i = start; i < stop; i += step)
. yield return i;
. }})
. }
. from x in seq(0, 3, 1)
. from y in seq(x, x + 5, 2)
. select new { x, y }
SelectManyIterator { \{ x = 0, y = 0 }, \{ x = 0, y = 2 }, \{ x = 0, y = 4 },
\{ x = 1, y = 1 }, \{ x = 1, y = 3 }, \{ x = 1, y = 5 },
\{ x = 2, y = 2 }, \{ x = 2, y = 4 }, \{ x = 2, y = 6 } }
>
The trick pulling this off is use of a local function (which is really the new C# 7 addition at work here) inside a statement lambda.
Happy Hacking!