How do I select the item with the highest value using LINQ?

Posted by mafutrct on Stack Overflow See other posts from Stack Overflow or by mafutrct
Published on 2010-06-09T08:37:19Z Indexed on 2010/06/09 8:42 UTC
Read the original article Hit count: 122

Filed under:
|
|

Imagine you got a class like this:

class Foo {
    string key;
    int value;
}

How would you select the Foo with the highest value from an IEnumeralbe<Foo>?

A basic problem is to keep the number of iterations low (i.e. at 1), but that affects readability. After all, the best I could find was something along the lines of this:

IEnumerable<Foo> list;
Foo max = list.Aggregate ((l, r) => l.value > r.value ? l : r);

Can you think of a more better way?

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ