LINQ SelectMany Projection Operator

In LINQ, the SelectMany operator is used to select values from a collection of collections, i.e., nested collection. It does this operation by itself, thereby reducing the lines of code that a normal program would typically have.

Syntax of LINQ SelectMany Projection Operator

Following is the syntax of using the LINQ SelectMany projection operator.

 

C# Code

 

var Subjects = Objstudent.SelectMany(x => x.Subjects);

VB.NET Code

 

Dim Subjects = Objstudent.SelectMany(Function(x) x.Subjects)

Let us understand SelectMany projection operator in LINQ with the help of an example.

LINQ SelectMany Projection Operator Example

We will create a sample console application in which we have a student class with few getters and setters properties and populate a strongly-typed List of class Student. After that, we will retrieve the data from the list using LINQ.

 

C# Code

 

using System;
using System.Collections.Generic;
using System.Linq;

namespace Linqtutorials
{
   class Program
   {
      static void Main(string[] args)
      {
         List<Student> Objstudent = new List<Student>()
         {
            new Student() { Name = "Ravi Varma", Gender = "Male", Subjects = new List<string> { "Mathematics", "Physics" } },
            new Student() { Name = "Vikram Sharma", Gender = "Male", Subjects = new List<string> { "Social Studies", "Chemistry" } },
            new Student() { Name = "Harish Dutt", Gender = "Male", Subjects = new List<string> { "Biology", "History", "Geography" } },
            new Student() { Name = "Akansha Wadhwani", Gender = "Female", Subjects = new List<string> { "English", "Zoology", "Botany" } },
            new Student() { Name = "Vikrant Seth", Gender = "Male", Subjects = new List<string> { "Civics", "Drawing" } }
         };
         var Subjects = Objstudent.SelectMany(x => x.Subjects);
         foreach (var Subject in Subjects)
         {
           Console.WriteLine(Subject);
         }
         Console.ReadLine();
      }
   }
   class Student
   {
      public string Name { get; set; }
      public string Gender { get; set; }
      public List<string> Subjects { get; set; }
   }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim Objstudent As New List(Of Student)() From {
New Student() With {.Name = "Ravi Varma", .Gender = "Male", .Subjects = New List(Of String)() From {"Mathematics", "Physics"}},
New Student() With {.Name = "Vikram Sharma", .Gender = "Male", .Subjects = New List(Of String)() From {"Social Studies", "Chemistry"}},
New Student() With {.Name = "Harish Dutt", .Gender = "Male", .Subjects = New List(Of String)() From {"Biology", "History", "Geography"}},
New Student() With {.Name = "Akansha Wadhwani", .Gender = "Female", .Subjects = New List(Of String)() From {"English", "Zoology", "Botany"}},
New Student() With {.Name = "Vikrant Seth", .Gender = "Male", .Subjects = New List(Of String)() From {"Civics", "Drawing"}}
}
Dim Subjects = Objstudent.SelectMany(Function(x) x.Subjects)
For Each Subject In Subjects
Console.WriteLine(Subject)
Next
Console.ReadLine()
End Sub

Class Student
Public Property Name() As String
Get
Return m_Name
End Get
Set(value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set(value As String)
m_Gender = Value
End Set
End Property
Private m_Gender As String
Public Property Subjects() As List(Of String)
Get
Return m_Subjects
End Get
Set(value As List(Of String))
m_Subjects = Value
End Set
End Property
Private m_Subjects As List(Of String)
End Class
End Module

If you observe the above code, we added a class Student which has few properties related to students, and we are trying to display the subjects that a student has. Here we used the SelectMany operator to get required subject values from multiple collections. Otherwise, we need to use various loops to get subject values. So by using the SelectMany projection operator in LINQ, we can avoid using numerous loops in the application.

LINQ SelectMany Operator Example Output

Following is the result of using the LINQ SelectMany operator in the application.

 

Mathematics
Physics
Social Studies
Chemistry
Biology
History
Geography
English
Zoology
Botany
Civics
Drawing

This is how we can use SelectMany projection operators in the application. Here, by using the SelectMany operator, we avoided two for each loop to iterate over the collection first and then in the List.