开发者

What's elegant way to rewrite following LINQ statement using extension methods?

I have 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 }
开发者_如何学C

One possible solution is:

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

However this is everything but elegant.

Do you any idea how to improve beauty of second expression?


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


Using Join is the wrong approach IMO.

The direct equivalent of this is (I think!):

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

Although I think it would be equivalent to:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜