I want to list the most purchased products by users by user ID.
My table looks like this
ID UserId ProductId
1 10 Apple
2 10 Computer
3 10 Computer
4 11 Apple
5 11 Apple
6 11 Computer
6 11 Phone
6 11 Phone
6 11 Phone
6 12 Fax
6 12 Fax
6 12 Phone
the output i wanted:
UserId: 10, MostPurchased: Computer
UserId: 11, MostPurchased: Phone
UserId: 12, MostPurchased: Fax
var mostRequestUsers = await dbContext.UserProducts.Include(x => x.Products)
.GroupBy(x => new { UserId = x.UserId, ProductName = x.Product.Name)
.OrderByDescending(gp => gp.Count())
.Select(g => new { Key = g.Key.UserId, RequestType = g.Key.ProductName }).ToListAsync();