CodexBloom - Programming Q&A Platform

LINQ GroupBy Not Grouping Correctly When Using SelectMany on Nested Collections

👀 Views: 53 💬 Answers: 1 📅 Created: 2025-07-07
c# linq groupby selectmany csharp

I'm encountering an issue with a LINQ query where I expect to group items based on a property from a nested collection, but the results are not what I anticipated. I'm using .NET 6.0 and have a list of `Order` objects, each containing a list of `OrderItem` objects, and I want to group the orders by the product name of the items. Here's a simplified version of my classes: ```csharp public class Order { public int OrderId { get; set; } public List<OrderItem> Items { get; set; } } public class OrderItem { public string ProductName { get; set; } public decimal Price { get; set; } } ``` And here's the LINQ query I'm using: ```csharp var groupedOrders = orders .SelectMany(order => order.Items.Select(item => new { order.OrderId, item.ProductName })) .GroupBy(x => x.ProductName) .Select(g => new { ProductName = g.Key, OrderIds = g.Select(o => o.OrderId).Distinct() }); ``` I expected `groupedOrders` to give me a collection where each entry corresponds to a unique product name and contains the list of associated order IDs. However, the output seems to include duplicate order IDs for the same product name, which doesn’t match the distinct orders I anticipated for each product. I've tried using `Distinct()` in different places within the query, but it hasn’t solved the problem. I also tried transforming the items before grouping, but it still leads to duplication. Here’s a sample of the data I’m testing with: ```csharp var orders = new List<Order> { new Order { OrderId = 1, Items = new List<OrderItem> { new OrderItem { ProductName = "Widget", Price = 10.0M }, new OrderItem { ProductName = "Gadget", Price = 15.0M }}}, new Order { OrderId = 2, Items = new List<OrderItem> { new OrderItem { ProductName = "Widget", Price = 10.0M }}}, new Order { OrderId = 3, Items = new List<OrderItem> { new OrderItem { ProductName = "Gadget", Price = 15.0M }, new OrderItem { ProductName = "Gizmo", Price = 20.0M }}}, }; ``` When I run this, I get: - For `Widget`, I see both Order IDs 1 and 2, which is correct. - For `Gadget`, I see Order IDs 1 and 3, which is not expected as I would like it to only show ID 1 (the actual distinct order for that product). - For `Gizmo`, I correctly get Order ID 3. How can I modify my LINQ query to ensure that I only get unique order IDs associated with each product name? Any insights would be greatly appreciated!