What an elegant way to rewrite the next LINQ statement using extension methods?

I have the following LINQ statement and I want to rewrite it using extension methods.

from x in e from y in e from z in e select new { x, z } 

One possible solution:

 e.Join(e, x => 42, y => 42, (x, y) => new { x, y }) Join(e, _ => 42, z => 42, (_, z) => new { _.x, z }); 

However, this is all but elegant.

Do you know how to improve the beauty of the second expression?

+7
c # linq
source share
2 answers
 e.SelectMany(x => e.SelectMany(y => e.Select(z => new { x, z }))) 
+6
source share

Using Join is the wrong IMO approach.

The direct equivalent of this (I think!):

 e.SelectMany(x => e.SelectMany(y => e.Select(new { y, z }), (x, yz) => new { x, yz.z })) 

Although I think this would be equivalent:

 e.SelectMany(x => e.SelectMany(y => e.Select(new { x, z }))) 
+6
source share

All Articles