LINQ SelectMany in C#

LINQ (Language-Integrated Query) is a powerful feature of C# that allows developers to easily work with data in a variety of formats. One of the most useful methods in the LINQ toolbox is SelectMany. This method is used to flatten collections and work with nested data structures. In this lesson, we will explore how to use LINQ SelectMany in C#.

What is LINQ SelectMany?

SelectMany is a LINQ method that flattens nested collections into a single collection. It is used to select and combine the elements of one or more collections in a sequence. The resulting sequence is a flattened version of the original nested collections.

Syntax:

The syntax for SelectMany is as follows:

public static IEnumerable<TResult> SelectMany<TSource, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, IEnumerable<TResult>> selector
)

The first parameter is the source collection, which is the collection of objects to be flattened. The second parameter is a selector function that takes each element of the source collection and returns a collection of objects that will be included in the final result.

Example:

Let’s take an example of a nested collection of objects:

List<List<int>> nestedList = new List<List<int>>()
{
    new List<int> {1, 2, 3},
    new List<int> {4, 5, 6},
    new List<int> {7, 8, 9}
};

Using SelectMany, we can flatten this nested collection into a single sequence of integers:

IEnumerable<int> flattenedList = nestedList.SelectMany(list => list);

// Output: 1, 2, 3, 4, 5, 6, 7, 8, 9

In this example, we pass a lambda expression to the SelectMany method. The lambda expression takes each list in the nested collection and returns the list itself. SelectMany then combines all the lists into a single sequence of integers.

Multiple Collection Example:

SelectMany can also be used to flatten multiple collections into a single sequence. Let’s take an example of a nested collection of objects that contains multiple collections:

List<List<int>> nestedList = new List<List<int>>()
{
    new List<int> {1, 2, 3},
    new List<int> {4, 5, 6},
    new List<int> {7, 8, 9}
};

List<List<int>> secondNestedList = new List<List<int>>()
{
    new List<int> {10, 11, 12},
    new List<int> {13, 14, 15},
    new List<int> {16, 17, 18}
};

Using SelectMany, we can flatten both the nested collections into a single sequence of integers:

IEnumerable<int> flattenedList = nestedList.SelectMany(list => list)
                                           .Concat(secondNestedList.SelectMany(list => list));

// Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18

In this example, we use the Concat method to combine the results of two SelectMany queries that flatten both the nested collections.

Conclusion:

In conclusion, SelectMany is a powerful method in the LINQ toolbox that allows developers to easily work with nested collections and flatten them into a single sequence. It is a useful tool when working with complex data structures in C# and can save a lot of time and effort when querying data.