How to find item with max value using linq?

C#LinqMax

C# Problem Overview


Have a look at the below table:

Item          Value
A                10
b                50
c                90

I want to find the item with maximum value. I can get that by using group by or orderding, but somehow I have a feeling there should be a more direct way. Am I right?

C# Solutions


Solution 1 - C#

With EF or LINQ to SQL:

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

With LINQ to Objects I suggest to use morelinq extension MaxBy (get morelinq from nuget):

var item = items.MaxBy(i => i.Value);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1784622View Question on Stackoverflow
Solution 1 - C#Sergey BerezovskiyView Answer on Stackoverflow