2017-11-18 104 views
0

客戶的字典和相關數據(例如MoneySpent),並想只返回客戶的排序列表。 我想通了迄今唯一的解決辦法是這樣的:更好的辦法排序字典並獲得鑰匙,我只有

CustomerData<Customer, int> //the value here is the money spent 
List<KeyValuePair<Customer, int>> sortedListByValue = CustomerData.OrderByDescending(s => s.Value).ToList(); 

然後只是要通過量清單,並獲得鑰匙。但是,我確信有一個更簡單的方法,並會很樂意提供建議。

+2

什麼是'CustomerData'?你能在你的代碼中顯示嗎? – CodingYoshi

+0

一種替代的解決方案,以接受的答案(當然,相同的真,但與其他語法),與查詢語法:'VAR排序=(從CustomerData的OrderBy kv.Value千伏降序選擇kv.Value).ToList(); ' –

回答

1

您可以選擇Key,這將給你的客戶。

var sortedListByValue = CustomerData.OrderByDescending(s => s.Value) 
    .Select(x => x.Key).ToList();