Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get N max numbers from a List<int> using lambda expression

This is my list:

List<int> numbers=new List<int> { 12, 5, -8, 4, 7, 28, 3, 22 };

How can I get 4 maximum numbers by lambda: I need these ones: {28, 22, 12, 7}

like image 978
Saeid Avatar asked Jan 30 '12 10:01

Saeid


1 Answers

Use:

var result = numbers.OrderByDescending(n => n).Take(4);
like image 168
Kirill Polishchuk Avatar answered Oct 14 '22 03:10

Kirill Polishchuk