linqquery-expressions

Find items with same property from list and pick the cheaper of the two items


I have a list of items each with a category and price associated with them. I wanted to know how to create a linq query that will first check for items with of the same category and then check which one of the two is cheaper and remove the more expensive item from the list?


Solution

  • items = items.GroupBy(x => x.category )
        .Select(x => new Item(x.Key, x.Select(y => y.price ).Min()))
        .ToList();