We have a collection of objects with a count property as follows...
Item A : Count 4
Item B : Count 2
Item C : Count 5 <-- We want this one
Item D : Count 3
(Note the actual list can have hundreds of items.)
We're trying to write a LINQ statement that returns 'Item C' since it has the highest value for Count.
Note: We don't want the count. We want the item with the highest count.
Of course this can be easily done with simple looping constructs, but I'm wondering if it can be achieved purely with LINQ.
Only thing I can think of is to use the Aggregate statement like this...
var item = Screen.Items
.Aggregate( (highestItem, nextItem) =>
highestItem = (highestItem == null)
? nextItem
: (highestItem.Count < nextItem.Count)
? nextItem
: highestItem);
Seems verbose though. Is there a different, shorter way?