获取item字段中较高值的对象



我正在使用这个表达式来获取最有价值的项目:

var notaMaisVelha = resultCrawler.Ranges.Max(c => c.Item);

问题是这个表达式返回item字段的值,而我想要这个对象。

如何获得具有最大项目字段的对象?

您可以按Item排序,并选择第一个项目,因为它将是最大的:

var notaMaisVelha = resultCrawler.Ranges.OrderByDescending(c => c.Item).First();

这将给出Ranges集合中Item的最大值的对象。

注意:

First()将抛出异常,如果集合中没有项目,还有另一个方法FirstOrDefault(),如果集合中没有项目,它将返回null,但在消费它时需要null检查。

用户按降序排序,取top 1

resultCrawler.Ranges.OrderByDescending(x => x.Item).FirstOrDefault();

最新更新