【问题标题】:Create an int[] from Dictionary<int, List<int>>, from each dictionary element's Value List<int> I need an array which contains all dictionary elements从 Dictionary<int, List<int>> 创建一个 int[],从每个字典元素的 Value List<int> 我需要一个包含所有字典元素的数组
【发布时间】:2011-05-17 09:24:21
【问题描述】:

我现在的解决方案是……

Dictionary<int, List<int>> oDict = <Some code to fill in the dictionary>;  

var oList = new List<int>();  
oDict.Values.ForEach(oList.AddRange);   
oList.ToArray();

有没有办法在不使用额外的List&lt;int&gt; 的情况下做到这一点?

【问题讨论】:

    标签: c# arrays dictionary


    【解决方案1】:

    是的,看起来像:

    var array = oDict.Values.SelectMany(list => list).ToArray();
    

    (如果您只想要不同的元素,只需在ToArray 之前调用Distinct。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 2021-10-27
      相关资源
      最近更新 更多