开发者

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)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜