Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get object with minimum value using extension method min()

Tags:

methods

c#

min

list.Min() gets me the min value as integer. I want to get the object in List which has the min value for a certain property X. How can I do that?

like image 206
msfanboy Avatar asked Dec 21 '22 12:12

msfanboy


1 Answers

Have a look at MinBy in MoreLINQ - or I believe Reactive Extensions has something similar in System.Interactive:

var cheapestProduct = products.MinBy(p => p.Price);

If more than one item has the lowest value, the earliest one in the sequence will be returned.

like image 119
Jon Skeet Avatar answered Jan 07 '23 06:01

Jon Skeet