LINQ offers various methods to transform and shape data, making it a versatile tool for developers. In this blog post, we’ll explore the SelectMany method and learn how it can be used to work with nested collections, flatten data structures, and perform advanced data manipulations.

Understanding the SelectMany Method

The SelectMany method is a powerful operator that is used to flatten nested collections and perform complex data manipulations in C#. It allows you to work with collections of collections and project them into a single, flattened sequence.

The SelectMany method takes two parameters: the source collection and a selector function. The selector function takes an element from the source collection and returns an enumerable collection of elements. The SelectMany method then flattens these enumerable collections into a single output sequence. In other words, it merges multiple collections into one.

Here is the basic syntax of the SelectMany method:

  • source: The source collection on which the SelectMany method is applied.
  • selector: A delegate function that defines the projection to be applied to each element of the source collection. It takes an element from the source collection and returns an enumerable collection of elements.
  • The result of the SelectMany method is an IEnumerable<TResult>, where TResult is the type of elements in the flattened sequence.

One of the most common use cases for the SelectMany method is flattening nested collections. Let’s consider an example where we have a collection of objects, each containing another collection of items.

In the above example, the SelectMany method is used to flatten the nested Products collection within each Order object. The result is an enumerable sequence of all the products across all orders.

LINQ SelectMany Projection Method

Here, you can see that the SelectMany method returns an IEnumerable<char>. This is because the SelectMany method returns all the elements from the sequence. Here the nameList is the sequence or collection or the data source. And the sequence contains two strings. And we know the string is a collection of characters. So, the SelectMany method fetches all the characters from the above two strings and then converts them into one sequence i.e. IEnumerable<char>.

LINQ SelectMany Using Query Syntax

The most important point that you need to remember is there is no such SelectMany Operator available in LINQ to write Query Syntax. In the below example, we are fetching all the strings from the nameList collection to str object and then we are using another from clause to fetch all the characters from the str object and then we are projecting the characters which are going to contain all the characters available in both strings.

SelectMany Projection Method with Complex Data Type

Let us create a class file with the name Student.cs and then copy and paste the following code into it. As you can see, we have created the following Student class with four properties. Further, if you notice the Programming property of the Student class returns List<string>. Here we have also created one method which will return the List of students which will be going to act as our data source.

The complete example code is given below. In the below example, I am showing both LINQ Query Syntax and Method Syntax to achieve the same.

When we execute the program, it will give us the following output. Here, you can see, it contains all the programming strings from all the students. Also, if you notice it is also containing the same string multiple times.

Remove Duplicates while using LINQ SelectMany Method

If you notice, in our previous example, we get the output as expected but with duplicate program names. If you want only the distinct program names then you need to apply the distinct method on the result set which is shown in the below examples. The LINQ Distinct Method is going to return distinct elements from a sequence by using the default equality comparer to compare values. In our upcoming articles, we will discuss Comparer and the Distinct Method in detail. For now, just remember distinct method is going to return distinct elements from a sequence.

LINQ SelectMany Method

Now, let us proceed and see more complex examples to understand the LINQ SelectMany Method. Our requirement is to retrieve the student’s name along with the program language names. The following example exactly does the same. For this, we are going to use the other overloaded version of the SelectMany method. In the below example, I am showing both Query and Method Syntax to achieve the same.

The SelectMany method is a valuable tool in the LINQ toolbox that allows you to work with nested collections and perform advanced data manipulations. By leveraging the power of SelectMany, you can flatten data structures, extract information from nested collections, and combine it with other LINQ operators to create powerful queries.

Remember to experiment with different scenarios and explore the capabilities of the SelectMany method. It can help you write more concise, expressive, and efficient code when working with collections in C#. Happy coding!


Leave a Comment