Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# sort dictionary with linq

Tags:

c#

linq

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>();

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

like image 910
Johan Avatar asked May 10 '11 12:05

Johan


1 Answers

You can do that using:

List<Product> productList = dictionary.OrderByDescending(kp => kp.Value)
                                      .Select(kp => kp.Key)
                                      .ToList();
like image 111
driis Avatar answered Oct 27 '22 14:10

driis