2010-09-24 97 views
0

我需要從存儲ArrayList的字典中檢索值,該ArrayList又有一個ArrayList第二個ArrayList具有存儲的int數組。現在我該如何檢索這些整數值。 `使用字典的lambda值從嵌套字典中獲取值是ArrayList的ArrayList

 Dictionary<int, ArrayList> obj = new Dictionary<int, ArrayList>(); 

     ArrayList objList1 = new ArrayList(); 

     ArrayList objList2 = new ArrayList(); 

     ArrayList objList3 = new ArrayList(); 

     Int32[] a1 = new Int32[5] {11, 21, 32, 43, 50 }; 
     Int32[] b1 = new Int32[5] { 123, 2321, 3212, 4983, 5760 }; 
     Int32[] c1 = new Int32[5] { 1341, 2991, 3552, 4663, 5880 }; 

     objList2.Add(a1); 
     objList2.Add(b1); 
     objList2.Add(c1); 



     objList1.Add(objList2); 
     objList1.Add(objList3); 

     obj.Add(1, objList1); 
     obj.Add(2, objList3);` 

這可以用List輕鬆完成。我試着用ArrayList解決。首先是可能的?提前致謝。

+0

問題獨立的堆棧溢出。在這裏寫下你的整個問題。並注意:這個問題沒有詳細說明。嘗試在這裏包含更多細節。 – 2010-09-24 05:50:41

+0

使用ArrayList比較痛苦,因爲它是非通用的。你確定你不能使用'List '而不是?很高興看到一個樣本,所以我們確切知道你有什麼和你想要的結果。 – 2010-09-24 05:55:30

+0

@Michael Petrotta:我編輯了我的問題,請讓我知道這是否已經被問到。 – Praneeth 2010-09-24 15:29:41

回答

1

你的意思是這樣的嗎?

foreach(var item in obj.Values 
    .SelectMany(x => x.Cast<ArrayList>()) 
    .SelectMany(x => x.Cast<int[]>()) 
    .SelectMany(x => x)) 
{ 
    Console.WriteLine(item); 
} 

輸出:

11 
21 
32 
43 
50 
123 
2321 
3212 
4983 
5760 
1341 
2991 
3552 
4663 
5880 
0
obj 
    .SelectMany(x=>x.Value.Cast<ArrayList>()) 
    .SelectMany(x=>x.Cast<int[]>()) 
    .SelectMany(x=>x)