2015-07-10 36 views
1

我想按鍵排序字典,然後從該字典中取出所有值,並將它們放入數組中。按鍵排序詞典,然後將所有值添加到列表

例如,

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

有好像應該是一個簡單的方法來做到這一點,但我完全不知道怎麼樣。謝謝你的時間!!

回答

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

你是聖人!非常感謝! –

+0

@returntrue很高興我能幫到你。 :) – Jashaszun

相關問題