List> nestedList = new List
> { new List
{ 1, 2, 3 }, new List { 4, 5, 6 }, new List { 7, 8, 9 } }; List flattenedList = nestedList.SelectMany(innerList => innerList).ToList(); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
class Order { public int OrderId { get; set; } public ListThe SelectMany method is part of the System.Linq namespace in the .NET Framework class library.LineItems { get; set; } } class LineItem { public int LineItemId { get; set; } public string ProductName { get; set; } public int Quantity { get; set; } } List orders = new List { new Order { OrderId = 1, LineItems = new List { new LineItem { LineItemId = 1, ProductName = "Product A", Quantity = 2 }, new LineItem { LineItemId = 2, ProductName = "Product B", Quantity = 3 }, } }, new Order { OrderId = 2, LineItems = new List { new LineItem { LineItemId = 3, ProductName = "Product A", Quantity = 1 }, new LineItem { LineItemId = 4, ProductName = "Product C", Quantity = 4 }, new LineItem { LineItemId = 5, ProductName = "Product D", Quantity = 2 }, } } }; List flattenedLineItems = orders.SelectMany(order => order.LineItems).ToList(); /* Output: [ { LineItemId = 1, ProductName = "Product A", Quantity = 2 }, { LineItemId = 2, ProductName = "Product B", Quantity = 3 }, { LineItemId = 3, ProductName = "Product A", Quantity = 1 }, { LineItemId = 4, ProductName = "Product C", Quantity = 4 }, { LineItemId = 5, ProductName = "Product D", Quantity = 2 } ] */