c# - 将字典转换为两个列表的最佳方法

标签 c# list dictionary ienumerable

如何转换 Dictionary<DateTime, double> 类型的字典至 Tuple<List<DateTime>, List<double>>

谢谢

编辑:以下是否保证两个列表中项目的顺序相同? var abc = new Tuple<List<DateTime>, List<double>>(_data.Keys.ToList(), _data.Values.ToList());

最佳答案

简单(保证顺序):

Tuple<List<DateTime>, List<double>> tuple 
                   = Tuple.Create(dict.Keys.ToList(), dict.Values.ToList());

the order of the values in the Dictionary.ValueCollection is unspecified, but it is the same order as the associated keys in the Dictionary.KeyCollection returned by the Keys property.

来源:MSDN

订单保证示例:

即使在更新、删除和添加之后也能保证顺序。

Dictionary<int, string> dict = new Dictionary<int, string>();

dict[2] = "2";
dict[1] = "0";
dict[3] = "3";
dict[1] = "1";
dict[1] = "1";

dict.Remove(3);

var tuple = Tuple.Create(dict.Keys.ToList(), dict.Values.ToList());
// 2 1
// "2" "1"

关于c# - 将字典转换为两个列表的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263037/

相关文章:

c# - 计数子记录,如果为空则显示零

list - Haskell <<循环>>

perl - 如何使用 map 将函数应用于列表?

Scala lambda 函数与 map 函数

c# - 使用回调从WCF发送混合声音

c# - 每个 VST SDK 似乎都适用于 C++,那么 C# 呢?

python - 单个列表中可能的列表组合

Python:对于每个列表元素,在列表中应用一个函数

Python dict 像满射多键→值容器

c# - IntPtr 算法