C#使用linq排序字典 [英] C# sort dictionary with linq

查看:597
本文介绍了C#使用linq排序字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一本字典:

I have a dictionary in C#:

public Dictionary<Product, int>

我想将结果放入通用列表中:

And I would like to get my result in to a generic list:

List<Product> productList = new List<Product>();

随着产品订购者以字典中的int值递减.我尝试使用orderby方法,但没有成功.

With the products orderder descending by the int value in the dictionary. I've tried using the orderby method, but without success.

推荐答案

您可以使用:

List<Product> productList = dictionary.OrderByDescending(kp => kp.Value)
                                      .Select(kp => kp.Key)
                                      .ToList();

这篇关于C#使用linq排序字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆