Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在项目字段中获取更高值的对象_C#_Linq - Fatal编程技术网

C# 在项目字段中获取更高值的对象

C# 在项目字段中获取更高值的对象,c#,linq,C#,Linq,我用这个表达式来获得最有价值的物品: var notaMaisVelha = resultCrawler.Ranges.Max(c => c.Item); 问题是这个表达式返回item字段的值,而我想要的是对象 如何获取项目字段最大的对象?您可以按项目对其进行排序,并选择第一个项目,因为它将是最大值: var notaMaisVelha = resultCrawler.Ranges.OrderByDescending(c => c.Item).First(); 这将为范围集合中的

我用这个表达式来获得最有价值的物品:

var notaMaisVelha = resultCrawler.Ranges.Max(c => c.Item);
问题是这个表达式返回item字段的值,而我想要的是对象

如何获取项目字段最大的对象?

您可以按项目对其进行排序,并选择第一个项目,因为它将是最大值:

var notaMaisVelha = resultCrawler.Ranges.OrderByDescending(c => c.Item).First();
这将为范围集合中的项提供最大值的对象

注: 如果集合中没有项目,First将引发异常。如果集合中没有项目,另一个方法FirstOrDefault将返回null,但在使用它时需要进行null检查。

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

var notaMaisVelha = resultCrawler.Ranges.OrderByDescending(c => c.Item).First();
这将为范围集合中的项提供最大值的对象

注:
如果集合中没有项目,First将引发异常;如果集合中没有项目,FirstOrDefault将返回null,但在使用它时需要进行null检查。

只需用户降序并取前1名

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

用户只需按降序排序,然后取前1名

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

我想你比我快了几秒钟:@johnny5耶看起来是这样的..我想你比我快了几秒钟:@johnny5耶看起来是这样。。