gpt4 book ai didi

c# - 从 Dictionary> 创建一个 int[],从每个字典元素的 Value List 我需要一个包含所有字典元素的数组

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:53 25 4
gpt4 key购买 nike

我现在的解决方案是...

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<int> 的情况下做到这一点? ?

最佳答案

是的,看起来像:

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

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

关于c# - 从 Dictionary<int, List<int>> 创建一个 int[],从每个字典元素的 Value List<int> 我需要一个包含所有字典元素的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com