Get the maximum value from a complex class

I have the following class:

class Seller
{
    private string sellerName;
    private decimal price;
}
~propreties for SellerName and Price goes here~

I also have a list of sellers:

list<Seller> s = new list<Seller>();

How can I get the maximum value pricefrom all sellers?

Many thanks.

+3
source share
1 answer

You can use linq as follows:

var max = s.Select(o => o.Price).Max();
//or this
var max = s.Max(o => o.Price);

For this to work, it pricemust be publicavailable.

You can also get the seller with the highest price, for example:

var maxPriceSeller = s.OrderByDescending(o => o.Price).First();

( priceis a property for the field price)

+5
source

Source: https://habr.com/ru/post/1740080/


All Articles