Mirza Fawad Baig
Mirza Fawad Baig

Reputation: 21

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.

Upvotes: 0

Views: 844

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27302

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();

Upvotes: 1

Related Questions