2010-09-24 75 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)