【问题标题】:get values from a nested dictionary using lambda values of the dictionary are ArrayList of ArrayList使用字典的 lambda 值从嵌套字典中获取值是 ArrayList 的 ArrayList
【发布时间】:2010-09-24 05:48:06
【问题描述】:

我需要从字典中检索值,该字典存储了一个 ArrayList,而后者又具有一个 ArrayList 这第二个 ArrayList 存储了 int 数组。现在我怎样才能检索那些整数值。 `

        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 解决它。首先可能吗?提前致谢。

【问题讨论】:

  • 问题在 Stack Overflow 上独立存在。在这里写下你的整个问题。请注意:这个问题没有明确说明。尝试在此处包含更多详细信息。
  • 使用 ArrayList 比较痛苦,因为它是非泛型的。您确定不能改用List&lt;T&gt; 吗?很高兴看到一个样本,这样我们就可以确切地知道您拥有什么以及您想要的结果。
  • @Michael Petrotta:我编辑了我的问题,如果有人问过,请告诉我。
  • @Jon Skeet:是的,使用 List 可以轻松完成,但对于这种情况,我使用 ArrayList。

标签: c# linq lambda arraylist


【解决方案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

【讨论】:

    【解决方案2】:
    obj
       .SelectMany(x=>x.Value.Cast<ArrayList>())
       .SelectMany(x=>x.Cast<int[]>())
       .SelectMany(x=>x)
    

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 2022-06-23
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      相关资源
      最近更新 更多