2015-08-16 189 views
0

我試圖將一些逗號分隔的行分割成最大大小爲2的組。 如何將組的集合轉換爲列表如下所示? 我預計分區在分組之後是3,然後是4。如何將IEnumerable <IEnumerable <IGrouping <int,string> >>轉換爲IEnumerable <IEnumerable <string>>

List<string> chunk = new List<string>() 
      { 
       "a,b,c", 
       "a,d,e", 
       "b,c,d", 
       "b,e,d", 
       "b,f,g", 
       "e" 
      }; 

      var partitons = chunk.GroupBy(c => c.Split(',')[0], (key, g) => g); 
      var groups = partitons.Select(x => x.Select((i, index) => new { i, index }).GroupBy(g => g.index/2, e => e.i)); 
      IEnumerable<IEnumerable<string>> parts = groups.Select(???) 

回答

0

試試這個:

partitons = groups.Select(x => x.SelectMany(y => y)); 

我得到這個:

partitons

+0

我想作爲輸出是什麼了IEnumerable > {{a,b,c},{a,d,e}}。 {{b,c,d},{b,e,d}}, {{b,f,g}}, {{e}} } – user330612

+0

這正是我的答案給你的。 – Enigmativity

+0

@ user330612 - 除了您的查詢不會將'{b,f,g}'分開。 – Enigmativity

1

這就是我想要的

var parts = groups.SelectMany(x => x).Select(y => y.Select(z => z)); 
相關問題