1

I'm using this expression to get the most valuable item:

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

The problem is that this expression returns the value of the item field and I wanted the object.

How do I get the object with the largest item field?

Thiago
  • 273
  • 1
  • 6
  • 24

2 Answers2

2

You can sort them by Item and pick the first item as it will be the max :

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

This will give the object which has the max value for the Item in Ranges collection.

Note:

First() will throw exception if there is no items in the collection, there is another method FirstOrDefault() which would return null if there is no item in the collection but will required null check when consuming it.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

Just user order by descending and take the top 1

resultCrawler.Ranges.OrderByDescending(x => x.Item).FirstOrDefault();
johnny 5
  • 19,893
  • 50
  • 121
  • 195