【问题标题】:Sort Dictionary by key then add all values to list按键排序字典,然后将所有值添加到列表
【发布时间】:2015-07-10 18:26:40
【问题描述】:

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

例如,

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";

似乎应该有一个简单的方法来做到这一点,但我完全不知道怎么做。感谢您的宝贵时间!

【问题讨论】:

    标签: c# linq list dictionary


    【解决方案1】:
    List<string> customerNames = (from c in customerNamesByIDs 
                                  orderby c.Key 
                                  select c.Value).ToList();
    

    【讨论】:

    • @returntrue 很高兴我能帮上忙。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多