我想按键对字典排序,然后从该字典中获取所有值并将它们放入数组中。

例如,

Dictionary<int, string> customerNamesByIDs = new Dictionary<int, string>();
customerNamesByIDs.Add(9, "joe");
customerNamesByIDs.Add(2, "bob");

//sort customerNamesByIDs by int
//take sorted dictionary and put them into an array
List<string> customerNames;
//customerNames[0] == "bob";
//customerNames[1] == "joe";


似乎应该有一种简单的方法来执行此操作,但是我绝对不知道如何执行此操作。谢谢你的时间!!

最佳答案

List<string> customerNames = (from c in customerNamesByIDs
                              orderby c.Key
                              select c.Value).ToList();

08-06 19:48