将Dictionary转换为两个列表的最佳方法

Mat*_*olf 2 c# ienumerable dictionary list

如何将类型转换Dictionary<DateTime, double>Tuple<List<DateTime>, List<double>>

谢谢

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

Zei*_*kki 5

简单(订单有保证):

Tuple<List<DateTime>, List<double>> tuple 
                   = Tuple.Create(dict.Keys.ToList(), dict.Values.ToList());
Run Code Online (Sandbox Code Playgroud)

Dictionary.ValueCollection中的值的顺序是未指定的,但它与Keys属性返回的Dictionary.KeyCollection中的关联键的顺序相同.

资料来源: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"
Run Code Online (Sandbox Code Playgroud)