How to find element with maximum value using linq?

Take a look at the table below:

Item Value A 10 b 50 c 90 

I want to find an element with maximum value . I can get this using group by or orderding , but somehow I feel that there should be a more direct way. I'm right?

+47
c # max linq
Mar 07 '13 at 7:38
source share
1 answer

With EF or LINQ to SQL:

 var item = db.Items.OrderByDescending(i => i.Value).FirstOrDefault(); 

With LINQ to Objects, I suggest using the morelinq extension MaxBy (get morelinq from nuget):

 var item = items.MaxBy(i => i.Value); 
+100
Mar 07 '13 at 7:40
source share



All Articles