59

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?

Dyrandz Famador
  • 4,303
  • 5
  • 18
  • 38
user1784622
  • 629
  • 1
  • 5
  • 9

1 Answers1

122

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);
matao
  • 627
  • 3
  • 14
  • 22
Sergey Berezovskiy
  • 215,927
  • 33
  • 392
  • 421
  • I can do it in your way, but I think there should be another way which is more direct to the problem. Is it really so? How about Max function of LinQ? – user1784622 Mar 07 '13 at 07:43
  • 3
    @user1784622 I think this is pretty straight forward. – sshow Mar 07 '13 at 07:45
  • 5
    @user1784622 Max function of LINQ will return max value, not item with max value. I.e. for your items you will get `90` – Sergey Berezovskiy Mar 07 '13 at 07:46
  • 6
    Yes, it's weird that Microsoft still haven't added MaxBy to the standard Enumerable extensions, given how many people want to use it. At least we have Jon Skeet's morelinq. :) – Matthew Watson Mar 07 '13 at 07:49
  • yes, thats exactly I was looking for, is there any way to use the morelinq in linqpad? – user1784622 Mar 07 '13 at 07:50
  • 2
    +1. If you can't for some reason use morelinq you can implement custom comparer and use corresponding version of .Net version of Max... – Alexei Levenkov Mar 07 '13 at 07:52
  • 1
    @user: You can reference your own libraries in Linqpad. There's an option to add references. – Matthew Watson Mar 07 '13 at 07:53
  • @user1784622 sorry, never tried to use third party assembly in linqpad. Morelinq is open source project, you can grab sources and use them in linqpad. Or you can use first option without morelinq – Sergey Berezovskiy Mar 07 '13 at 07:53
  • ok guys, I think I got everything! thanks for the help. – user1784622 Mar 07 '13 at 07:55
  • 1
    You can use the LINQ Aggregate method to find the object with the corresponding max or min value. Say you want minimum and your object has two members, Item and Distance. This finds the item with the minimum distance: list.Aggregate((a,b) => a.Distance <= b.Distance ? a : b).Item; This should be more time and space efficient than sorting as it makes a single pass and does not have to hold the whole list like it would during the sort. – Paul Chernoch Jun 23 '15 at 14:20
  • 1
    Why mentioning `EF` and `LINQ to SQL` and `LINQ to Objects`? It's just a LINQ operation/method on a collection/enumerable. It doesn't matter where that collection/enumerable is actually from/actually represents. – Don Cheadle Mar 25 '16 at 17:44
  • I love how much better it is to do `orderby+first` than doing something like `first(value = db.items.max(field1)).field2`, especially if you have some complex joins with that table. – Andrew Dec 19 '17 at 15:53
  • Surely there is a non-morelinq way to get the max element without sorting the whole list? – Meekohi Apr 28 '21 at 20:54
  • @Meekohi neither of these options sorts the whole list. In the first case, there is no list - it's a database query – Sergey Berezovskiy Apr 29 '21 at 16:56
  • I was referring to the non-db situation of course... – Meekohi May 05 '21 at 20:56