Search code examples
c#linqgroup-bygreatest-n-per-group

C# LINQ: Get items with max price


I have a list of my objects:

class MyObj
{
    public String Title { get; set; }
    public Decimal Price { get; set; }
    public String OtherData { get; set; }
}

var list = new List<MyObj> {
 new MyObj { Title = "AAA", Price = 20, OtherData = "Z1" },
 new MyObj { Title = "BBB", Price = 20, OtherData = "Z2" },
 new MyObj { Title = "AAA", Price = 30, OtherData = "Z5" },
 new MyObj { Title = "BBB", Price = 10, OtherData = "Z10" },
 new MyObj { Title = "CCC", Price = 99, OtherData = "ZZ" }
};

What is the best way to get list with unique Title and MAX(Price). Resulting list needs to be:

var ret = new List<MyObj> {
 new MyObj { Title = "BBB", Price = 20, OtherData = "Z2" },
 new MyObj { Title = "AAA", Price = 30, OtherData = "Z5" },
 new MyObj { Title = "CCC", Price = 99, OtherData = "ZZ" }
};

Solution

  • Well, you could do:

    var query = list.GroupBy(x => x.Title)
                    .Select(group =>
                    {
                        decimal maxPrice = group.Max(x => x.Price);
                        return group.Where(x => x.Price == maxPrice)
                                    .First();
                    };
    

    If you need LINQ to SQL (where you can't use statement lambdas) you could use:

    var query = list.GroupBy(x => x.Title)
           .Select(group => group.Where(x => x.Price == group.Max(y => y.Price))
                                 .First());
    

    Note that in LINQ to Objects that would be less efficient as in each iteration of Where, it would recompute the maximum price.

    Adjust the .First() part if you want to be able return more than one item with a given name if they both have the same price.

    Within LINQ to Objects you could also use MoreLINQ's MaxBy method:

    var query = list.GroupBy(x => x.Title)
                    .Select(group => group.MaxBy(x => x.Price));