'GroupBy with Where clause LINQ query

I have a question, I want to use Group by with Where clause. Here is the scenario, I want get all the orders of a user and group by them with ordernumber so it doesn't show me multiple orders with same order number. I tried to use LINQ query but that doesn't seemed to work. Does anyone have any idea?

var result = Entity.Where(x => x.UserId == Guid.Parse(userId)).GroupBy(x => x.OrderNumber).ToList();

This is what I tried.



Solution 1:[1]

You can get first record from group.

var result = Entity
   .Where(x => x.UserId == Guid.Parse(userId))
   .GroupBy(x => x.OrderNumber)
   .Select(g => g.First())
   .ToList();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Svyatoslav Danyliv