How can I get LINQ to return the object which has the max value for a given property? [duplicate]

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.

10 Answers
10

Leave a Comment