从复杂的类中获取最大值

iTa*_*ayb 3 c#

我有以下课程:

class Seller
{
    private string sellerName;
    private decimal price;
}
~propreties for SellerName and Price goes here~
Run Code Online (Sandbox Code Playgroud)

我还有一份卖家名单:

list<Seller> s = new list<Seller>();
Run Code Online (Sandbox Code Playgroud)

如何price从所有卖家中获得最大价值?

非常感谢你.

Nic*_*ver 5

您可以像这样使用linq:

var max = s.Select(o => o.Price).Max();
//or this
var max = s.Max(o => o.Price);
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,price需要这样才能实现public.

您也可以以最高价格获得卖家,如下所示:

var maxPriceSeller = s.OrderByDescending(o => o.Price).First();
Run Code Online (Sandbox Code Playgroud)

(Price作为你所在price领域的财产)

  • 这两个语句返回相同的东西,即表示最大价格的int.您不会使用第二个声明来获得卖家. (2认同)