我正在尝试编号列表的编号组。 例如,这个LINQ查询给出了我想要的内容
(from word in "The quick brown fox jumps over the lazy dog" .Split()
group word by word.Length into w
select w)
.Select((value, index) => new { i = index + 1, value })
.SelectMany(
sm => sm.value,
(sm, s) => new { sm.i, s})
1 The
1 fox
1 the
1 dog
2 quick
2 brown
2 jumps
3 over
3 lazy
但我决定优化这个查询:为什么我们需要使用外部的SelectMany索引,如果它在SelectMany的第4次重载中有自己的索引? 我尝试以下一种方式使用此重载,但我没有看到解决方案。
(from word in "The quick brown fox jumps over the lazy dog".Split()
group word by word.Length into w
select w)
.SelectMany(
(source, index) => ??????,
(msource, coll) => ??????)
答案 0 :(得分:4)
SelectMany
的{p> This overload应该有效:
(from word in "The quick brown fox jumps over the lazy dog".Split()
group word by word.Length into g
select g)
.SelectMany((g, i) => g.Select(word => new { index = i + 1, word }))