2013-09-26 6 views
5

リストの番号付きグループを作成しようとしています。 たとえば、このLINQクエリは、私がSelectManyでインデックスを使用する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インデックスに外部使う必要があるのはなぜ? そして、このオーバーロードを次の方法で使用しようとしましたが、解決策が表示されません。

SelectMany
(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) => ??????) 

答えて

3

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 })) 
+0

グレート!期待以上に簡単です。 –

関連する問題