Avatar ·

Grouping by multiple columns using LINQ in C#

Hi everyone! I'm new to C# and LINQ, and I've encountered a problem when grouping data. I have a list of orders (List), where each order contains the fields: CustomerId, ProductId, Quantity, and Price. I need to group orders by two fields simultaneously — by CustomerId and ProductId — to get the total quantity and sum for each customer and product. Here's my class: ```csharp public class Order { public int CustomerId { get; set; } public int ProductId { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } } ``` I tried using GroupBy with one field, for example: ```csharp var grouped = orders.GroupBy(o => o.CustomerId); ``` This works, but I need to group by two fields at once. I've read that you can use