如何转换 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/