Get the maximum value out of an complex class
I have the following class:
class Seller
{
private string sellerName;
private decimal price;
}
~propreties for SellerName and Price goes here~
I also hav开发者_如何学Pythone a list of sellers:
list<Seller> s = new list<Seller>();
How can I get the maximum value of price
out of all the sellers?
Thank you very much.
You can use linq like this:
var max = s.Select(o => o.Price).Max();
//or this
var max = s.Max(o => o.Price);
For this to work though, price
needs to be public
so it's accessible.
You can also get the seller with the maximum price, like this:
var maxPriceSeller = s.OrderByDescending(o => o.Price).First();
(Price
being the property for your price
field)
精彩评论