2015-06-30 75 views
-1

我有以下變量:如何在一個陣列中合併三維陣列

IEnumerable<Comment>[][] moderatedComments; 
/** 
* First dimension [] = The repository from where I fetched the comments (Could be more than one repository) 
* Second dimension [][] = The moderation operation (There can only be two operations, so this dimension is always of size 2) 
* Third dimension IEnumerable<Comment> = The moderated comments for that dimension 
*/ 

一個例子:含有

Comment comment = moderatedComments[1][0].First(); 
// Repository 1 
// Moderation operation 0 
// First moderated comment for that repository and operation 

欲所有三個維度合併成一個(IEnumerable<Comment>)中的所有審覈意見,無論存儲庫或審覈操作如何。

LINQ如何實現?

+3

這不是一個多維數組,它僅僅是一個數組的數組... – leppie

+0

@leppie對不起,我不知道確切的單詞爲這些類型的數組,我會很樂意接受標題編輯 –

+6

你能詳細說明爲什麼你使用嵌套數組來表示你的對象圖嗎?你可能會更好地爲你的'Comment'類添加屬性。 –

回答

4

像這樣做,請:

IEnumerable<Comment>[][] moderatedComments; 
var merged = moderatedComments.SelectMany(x => x).SelectMany(x => x); 
// merged is IEnumerable<Comment> 
+0

最後的評論是非常有用的,但用確切的類型替換'var'是一個更強的說法。某些生產守則指導方針不必在答案中遵循。 –