SelectMany Linq Example

If you familiar with the Linq Select operator, then the SelectMany is very similar. It might be confusing at first, but SelectMany is used to flatten a nested list or array.

A basic Example

Let's say you have an array of people and each person has an array of orders. See the nesting here? Now, how do we just select the orders from all people? This is where SelectMany comes in use.

Method syntax

Here we have a list of people. Within each person has an array of orders. To project all orders from all people we can use the lambda expression person => person.Orders.

Live example: https://dotnetfiddle.net/HdxGdX

var people = new[]
{
    new
        {
            Name = "Vernon",
            Age = 21,
            Email = "[email protected]",
            Phone = "806-291-8721",
            Orders = new[]
                {
                    new {OrderId = "AB231", Sku = "FB1232", Price = 9.95},
                    new {OrderId = "AA231", Sku = "FB5678", Price = 9.95},
                    new {OrderId = "BB231", Sku = "FB6366", Price = 9.95},
                }
        },
    new
        {
            Name = "Carrie",
            Age = 22,
            Email = "[email protected]",
            Phone = "617-389-2329",
            Orders = new[]
                {
                    new {OrderId = "XF231", Sku = "QA123211", Price = 24.95}
                }
        },
    new
        {
            Name = "Thomas",
            Age = 23,
            Email = "[email protected]",
            Phone = "906-875-5259",
            Orders = new[]
                {
                    new {OrderId = "UU231", Sku = "NM1231228", Price = 59.95}
                }
        }
};


// select all orders from everyone

var allOrders = people.SelectMany(person => person.Orders);

//output:
// OrderId: AB231 Sku:FB1232 Price:9.95
// OrderId: AA231 Sku:FB5678 Price:9.95
// OrderId: BB231 Sku:FB6366 Price:9.95
// OrderId: XF231 Sku:QA123211 Price:24.95
// OrderId: UU231 Sku:NM1231228 Price:59.95

Query syntax

With the query syntax, we have to add an additional from statement such as from orders in person.Orders. Then we just simply select the orders.

Live example: https://dotnetfiddle.net/3kEfU8

var people = new[]
{
    new
        {
            Name = "Vernon",
            Age = 21,
            Email = "[email protected]",
            Phone = "806-291-8721",
            Orders = new[]
                {
                    new {OrderId = "AB231", Sku = "FB1232", Price = 9.95},
                    new {OrderId = "AA231", Sku = "FB5678", Price = 9.95},
                    new {OrderId = "BB231", Sku = "FB6366", Price = 9.95},
                }
        },
    new
        {
            Name = "Carrie",
            Age = 22,
            Email = "[email protected]",
            Phone = "617-389-2329",
            Orders = new[]
                {
                    new {OrderId = "XF231", Sku = "QA123211", Price = 24.95}
                }
        },
    new
        {
            Name = "Thomas",
            Age = 23,
            Email = "[email protected]",
            Phone = "906-875-5259",
            Orders = new[]
                {
                    new {OrderId = "UU231", Sku = "NM1231228", Price = 59.95}
                }
        }
};


// select all orders from everyone

var allOrders = from person in people
                from orders in person.Orders
                select orders;

//output:
// OrderId: AB231 Sku:FB1232 Price:9.95
// OrderId: AA231 Sku:FB5678 Price:9.95
// OrderId: BB231 Sku:FB6366 Price:9.95
// OrderId: XF231 Sku:QA123211 Price:24.95
// OrderId: UU231 Sku:NM1231228 Price:59.95