【问题标题】:How to get dictionary values as a generic list如何获取字典值作为通用列表
【发布时间】:2011-11-25 05:20:27
【问题描述】:

我只想从字典值中获取一个列表,但这并不像看起来那么简单!

这里是代码:

Dictionary<string, List<MyType>> myDico = GetDictionary();
List<MyType> items = ???

我试试:

List<MyType> items = new List<MyType>(myDico.values)

但它不起作用:-(

【问题讨论】:

  • 在什么情况下不起作用?你有错误吗?
  • 字典是映射到List&lt;MyType&gt; 对象的键的集合。所以你在字典中有多个List&lt;MyType&gt;s。您是否尝试将所有 MyTypes 列表扁平化为一个列表?
  • 在字典中运行一个循环来填充一个项目列表——看起来很简单——不过我不知道是否有一个步骤。

标签: c# list dictionary


【解决方案1】:

怎么样:

var values = myDico.Values.ToList();

【讨论】:

  • @J.Chang,这不会使值变平,因此使用dic.Values.ToList() 您将生成List&lt;List&lt;MyType&gt;&gt; 而不是展平列表。这仅对每个键上的单个值有效。
【解决方案2】:

当然,myDico.Values 是 List&lt;List&lt;MyType&gt;&gt;

如果你想扁平化你的列表,请使用 Linq

var items = myDico.SelectMany (d => d.Value).ToList();

【讨论】:

  • 其实Values的类型应该是Dictionary&lt;string, List&lt;MyType&gt;&gt;.ValueCollection
  • 有谁知道为什么要这样实现?让 myDico.Values 返回 List> 更有意义,因为它在 Java 中 - 或者我错过了什么?
【解决方案3】:

您可能希望将Values 中的所有列表扁平化为一个列表:

List<MyType> allItems = myDico.Values.SelectMany(c => c).ToList();

【讨论】:

    【解决方案4】:

    另一种变体:

    List<MyType> items = new List<MyType>();
    items.AddRange(myDico.Values);
    

    【讨论】:

    • 在没有真正阅读问题的情况下看到你能获得多少赞成票,真是太棒了。尽管您回答了我来这里时提出的问题,但 OP 实际上还问了其他问题(Dictionary 包含 Lists)。
    【解决方案5】:

    我的 OneLiner:

    var MyList = new List<MyType>(MyDico.Values);
    

    【讨论】:

    • 对我来说效果很好!。谢谢。
    • 我猜这不会将字典中的列表扁平化为一个列表。
    • 为我工作,对结果更糟的人感到抱歉。太糟糕了。我希望其中一个对你有用!
    • 直到我将其更改为 var MyList = new List>(dict.Values);
    【解决方案6】:

    进一步了解 Slaks 的答案,如果您的字典中有一个或多个列表为空,则在调用 ToList() 时会抛出 System.NullReferenceException,请注意安全:

    List<MyType> allItems = myDico.Values.Where(x => x != null).SelectMany(x => x).ToList();
    

    【讨论】:

      【解决方案7】:
      Dictionary<string, MyType> myDico = GetDictionary();
      
      var items = myDico.Select(d=> d.Value).ToList();
      

      【讨论】:

        【解决方案8】:

        使用这个:

        List<MyType> items = new List<MyType>()
        foreach(var value in myDico.Values)
            items.AddRange(value);
        

        问题是字典中的每个键都有一个实例列表作为值。如果每个键都只有一个实例作为值,您的代码就可以工作,如下例所示:

        Dictionary<string, MyType> myDico = GetDictionary();
        List<MyType> items = new List<MyType>(myDico.Values);
        

        【讨论】:

          【解决方案9】:
                  List<String> objListColor = new List<String>() { "Red", "Blue", "Green", "Yellow" };
                  List<String> objListDirection = new List<String>() { "East", "West", "North", "South" };
          
                  Dictionary<String, List<String>> objDicRes = new Dictionary<String, List<String>>();
                  objDicRes.Add("Color", objListColor);
                  objDicRes.Add("Direction", objListDirection);
          

          【讨论】:

            【解决方案10】:

            您也可以使用另一种变体

            MyType[] Temp = new MyType[myDico.Count];
            myDico.Values.CopyTo(Temp, 0);
            List<MyType> items = Temp.ToList();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-11-02
              • 2020-08-06
              • 2022-12-01
              • 2012-03-03
              • 2013-08-22
              相关资源
              最近更新 更多