194

If I have a class that looks like:

public class Item
{
    public int ClientID { get; set; }
    public int ID { get; set; }
}

And a collection of those items...

List<Item> items = getItems();

How can I use LINQ to return the single "Item" object which has the highest ID?

If I do something like:

items.Select(i => i.ID).Max(); 

I'll only get the highest ID, when what I actually want returned is the Item object itself which has the highest ID? I want it to return a single "Item" object, not an int.

1
  • in addition to the answers on this page i thought it worth mentioning this answer too: stackoverflow.com/a/1101979/4880924 can anyone answer better than Jon Skeet? Provides reasoning for his answer.
    – BenKoshy
    Nov 17, 2016 at 22:58

10 Answers 10

210

This will loop through only once.

Item biggest = items.Aggregate((i1,i2) => i1.ID > i2.ID ? i1 : i2);

Thanks Nick - Here's the proof

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<Item> items1 = new List<Item>()
        {
            new Item(){ ClientID = 1, ID = 1},
            new Item(){ ClientID = 2, ID = 2},
            new Item(){ ClientID = 3, ID = 3},
            new Item(){ ClientID = 4, ID = 4},
        };
        Item biggest1 = items1.Aggregate((i1, i2) => i1.ID > i2.ID ? i1 : i2);

        Console.WriteLine(biggest1.ID);
        Console.ReadKey();
    }


}

public class Item
{
    public int ClientID { get; set; }
    public int ID { get; set; }
}  

Rearrange the list and get the same result

8
  • 1
    I upvoted this because I like the concept. I have no idea if the this code will actually do what was asked however. Jul 6, 2010 at 17:50
  • 1
    I have had this same problem occur more than once, and this is the most elegant solution I have found. Thanks!
    – Justin R.
    Apr 11, 2012 at 21:03
  • 3
    Well, Item itemBig = items1.Count() > 0 ? items1.Aggregate((i1, i2) => i1.ID > i2.ID ? i1 : i2) : null;
    – ruffin
    Nov 22, 2014 at 18:22
  • 5
    @ruffin I would use items1.Any(). That way you don't iterate the entire collection. Feb 24, 2015 at 19:46
  • 1
    This should be the accepted answer. It works perfectly, as you get to control how the "aggregation" happens you can simply always just return the instance with the highest/lowest thing being measured, or for that matter, any other comparison you like.
    – Jez
    Dec 27, 2020 at 18:21
106
.OrderByDescending(i=>i.id).First()

Regarding the performance concern, it is very likely that this method is theoretically slower than a linear approach. However, in reality, most of the time we are not dealing with the data set that is big enough to make any difference.

If performance is a main concern, Seattle Leonard's answer should give you linear time complexity. Alternatively, you may also consider to start with a different data structure that returns the max value item at constant time.

First() will do the same as Take(1) but returns the item directly instead of an enumeration containing the item.

12
  • 16
    Works, but it's nlogn instead of linear time.
    – tzaman
    Jul 6, 2010 at 17:36
  • 4
    tzaman: in theory, the LINQ system could identify the "orderby().take()" pattern and use a linear-time algorithm -- but you're right that it probably doesn't.
    – Gabe
    Jul 6, 2010 at 17:54
  • 46
    .OrderByDescending(i=>i.id).First() would return the object itself, rather than an enumeration with 1 item.
    – steve cook
    Jun 4, 2014 at 1:53
  • 2
    I know this is super old, but @stevecook .FirstOrDefault() is probably a better choice, since it's a little less prone to issues.
    – Dortimer
    May 1, 2019 at 16:55
  • 7
    @Dortimer if that's a valid error, then that's the opposite of safer. it should be brought to everyone's attention. "Error hiding" is a bad habit (an anti-pattern). i occasionally still use FirstOrDefault() when i expect one element, but it's only to be able produce a more informative exception message than the default
    – symbiont
    Jun 16, 2019 at 19:35
40
int max = items.Max(i => i.ID);
var item = items.First(x => x.ID == max);

This assumes there are elements in the items collection of course.

6
  • 1
    "Where" returns all items with the max value, maybe we want only the first, in that case "First" would be the best
    – digEmAll
    Jul 6, 2010 at 17:45
  • 1
    +1 for the clarity; I'm in the camp tzaman describes above (and may try morelinq anyway too...)
    – goodeye
    Dec 22, 2011 at 0:20
  • This code returns item with max ID. As Jon Skeet said, Max returns the maximal value, not the item containing the maximal value. Sep 3, 2015 at 7:16
  • immediately readable, and requires no new lib/extension Mar 25, 2016 at 17:32
  • 4
    This answer does unnecessary work. The list is completely iterated over in the first call to Max. The next call to First will perform another iteration over the list to find the element.
    – antiduh
    Nov 28, 2016 at 23:39
37

Use MaxBy from the morelinq project:

items.MaxBy(i => i.ID);
6
  • 10
    @Reed: I guess you've worked out why now... but for other readers: Max returns the maximal value, not the item containing the maximal value. Note that MaxBy is in System.Interactive in the Reactive Extensions framework too.
    – Jon Skeet
    Jul 6, 2010 at 17:40
  • 1
    @Jon: Yeah - I always forget that one - I keep using it thinking it's correct, too Jul 6, 2010 at 17:43
  • 1
    No need to write your own method (or add another dependency); Seattle Leonard and NickLarson both give simple one-liners which do the same thing. Jul 6, 2010 at 19:36
  • 2
    @BlueRaja: morelinq has so many useful functions I throw it in on almost every project anyway. :) Also, I find MaxBy to be much clearer in intent than the equivalent Aggregate syntax - decreasing mental parse-time is always beneficial later down the line. Sure, anyone with functional experience will recognize the fold just as fast, but not everyone has that. Finally, NickLarsen's solution does two passes (and has a problem if there are multiple max-values).
    – tzaman
    Jul 7, 2010 at 8:17
  • @tzaman Your solution would have the same problem as NickLarsen's if there are multiple max-values, wouldn't it? At some point, the code needs to pick which one is "correct" if there are multiple with the max-value. Mar 25, 2016 at 17:30
10

This is an extension method derived from @Seattle Leonard 's answer:

 public static T GetMax<T,U>(this IEnumerable<T> data, Func<T,U> f) where U:IComparable
 {
     return data.Aggregate((i1, i2) => f(i1).CompareTo(f(i2))>0 ? i1 : i2);
 }
1
  • In something like this there should always be a using example. Aug 12, 2022 at 8:28
6

In case you don't want to use MoreLINQ and want to get linear time, you can also use Aggregate:

var maxItem = 
  items.Aggregate(
    new { Max = Int32.MinValue, Item = (Item)null },
    (state, el) => (el.ID > state.Max) 
      ? new { Max = el.ID, Item = el } : state).Item;

This remembers the current maximal element (Item) and the current maximal value (Item) in an anonymous type. Then you just pick the Item property. This is indeed a bit ugly and you could wrap it into MaxBy extension method to get the same thing as with MoreLINQ:

public static T MaxBy(this IEnumerable<T> items, Func<T, int> f) {
  return items.Aggregate(
    new { Max = Int32.MinValue, Item = default(T) },
    (state, el) => {
      var current = f(el.ID);
      if (current > state.Max) 
        return new { Max = current, Item = el };
      else 
        return state; 
    }).Item;
}
5

Or you can write your own extension method:

static partial class Extensions
{
    public static T WhereMax<T, U>(this IEnumerable<T> items, Func<T, U> selector)
    {
        if (!items.Any())
        {
            throw new InvalidOperationException("Empty input sequence");
        }

        var comparer = Comparer<U>.Default;
        T   maxItem  = items.First();
        U   maxValue = selector(maxItem);

        foreach (T item in items.Skip(1))
        {
            // Get the value of the item and compare it to the current max.
            U value = selector(item);
            if (comparer.Compare(value, maxValue) > 0)
            {
                maxValue = value;
                maxItem  = item;
            }
        }

        return maxItem;
    }
}
3

try this:

var maxid = from i in items
            group i by i.clientid int g
            select new { id = g.Max(i=>i.ID }
3

In LINQ you can solve it the following way:

Item itemMax = (from i in items
     let maxId = items.Max(m => m.ID)
     where i.ID == maxId
     select i).FirstOrDefault();
1

You could use a captured variable.

Item result = items.FirstOrDefault();
items.ForEach(x =>
{
  if(result.ID < x.ID)
    result = x;
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.