更好的方法,然后对字典进行排序并仅获取密钥



我有一个客户和相关数据的字典(例如MoneySpent(,并且只想返回排序的客户列表。到目前为止,我想出的唯一解决方案是:

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

然后只需浏览列表并获取密钥。但是,我相信有一种更简单的方法,并且很乐意提供建议。

您可以选择

将为您提供客户的Key

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

最新更新