开发者

Get the maximum value out of an complex class

开发者 https://www.devze.com 2022-12-25 16:48 出处:网络
I have the following class: class Seller { private string sellerName; private decimal price; } ~propreties for SellerName and Price goes here~

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号