2013-02-19 94 views
-5

我有這樣的建築列表與陣列

List<int[]> nodeIds = new List<int[]>(); 

我可以得到每個元素從int[]?我該怎麼辦呢?

+0

學習一些編程的基礎知識,問這樣的問題之前, – Nickolaus 2013-02-19 08:44:10

回答

0

您有Listint[]所以您需要做的是獲取List中每個項目的每個元素。你必須使用嵌套循環 - 一個在列表上,另一個在當前考慮的列表項的數組上。

1

您可以使用LINQ Enumerable.SelectMany方法拉平序列合併爲一個序列:

IEnumerable<int> allIds = nodeIds.SelectMany(x => x); 

更新:如果您需要在產生的序列只有唯一的ID,然後諂媚序列之後申請也Enumerable.Distinct

0

類似:

foreach(var nodeId in nodeIds) 
{ 
    foreach(var id in nodeId) 
    { 
     //do stuff with id 
    } 
} 

nodeIds[0][0] 
3

例如:

foreach(int[] items in nodeIds) 
    foreach(int item in items) 
    // do something with item 
0

如果你想從列表中的所有陣列的所有整數,則可以使用SelectMany方法IEnumerable

IEnumerable<int> allIDs = nodeIds.SelectMany(a => a); 

可以使用foreach枚舉結果或對結果進行彙總等