C# 从复杂类中获取最大值

C# 从复杂类中获取最大值,c#,C#,我有以下课程: class Seller { private string sellerName; private decimal price; } ~propreties for SellerName and Price goes here~ 我还有一份卖家名单: list<Seller> s = new list<Seller>(); list s=新列表(); 如何从所有卖家那里获得price的最大价值 非常感谢。您可以这样使用linq: va

我有以下课程:

class Seller
{
    private string sellerName;
    private decimal price;
}
~propreties for SellerName and Price goes here~
我还有一份卖家名单:

list<Seller> s = new list<Seller>();
list s=新列表();
如何从所有卖家那里获得
price
的最大价值


非常感谢。

您可以这样使用linq:

var max = s.Select(o => o.Price).Max();
//or this
var max = s.Max(o => o.Price);
var maxPriceSeller = s.OrderByDescending(o => o.Price).First();
但是,要使其正常工作,
price
必须是
public
才能访问

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

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

Price
是您的
Price
字段的属性)

我必须使用属性还是数据成员本身?请注意,
Max()
系统.Linq
命名空间的一部分。@iTayb-属性或字段中的任何一个都有效,只要它是公共的,就会更新示例以使用您的属性,类似于
public double Price{get;set;}
或example的语句。这两条语句返回相同的内容,即一个表示最高价格的int。你不能用第二条语句得到卖家。@西蒙-很好,我忘了它是
Func
,现在更新了。