What is the best way to apply SelectMany
to get a cross join of three or more sequences using only extension methods? Is there any other way to get a cross join?
Test Data
var a = Enumerable.Range(11, 2);
var b = Enumerable.Range(21, 2);
var c = Enumerable.Range(31, 2);
Expected Result
X Y Z
11 21 31
11 21 32
11 22 31
11 22 32
12 21 31
12 21 32
12 22 31
12 22 32
What I tried
Here's the code that works but I wonder if there's any alternative that'd be easier to read and understand:
var d = a
.SelectMany(rb => b
.SelectMany(rc => c, (y, z) => new { Y = y, Z = z}),
(x, yz) => new { X = x, Y = yz.Y, Z = yz.Z });
The equivalent query expression is good but not what I'm looking for:
var e = from x in a
from y in b
from z in c
select new { X = x, Y = y, Z = z };