LINQ SelectMany Projection Operator

  Total Views : 308
  Comments : 0
 
  Keywords : Projection in linq with selectmany operation in c# / vb.net with example, How to use selectmany projection operator in linq with example, Linq selectmany operator query syntax in c# / vb.net

Here we will learn linq selectmany projection operator in c# / vb.net with example and how to use linq selectmany projection operator to select data from collection of collection with examples.

LINQ SelectMany Projection Operator

In LINQ SelectMany operator is used to select values from a collection of collection 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 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. And then 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()

EndSub

 

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 above code we added a class Student which has few properties related to students and we are trying to to display the subjects that a student has. Here we used SelectMany operator to get required subject values  from multiple collections otherwise we need to use multiple loops to get subject values. So by using SelectMany projection operator in LINQ we can avoid using multiple loops in application.

LINQ SelectMany Operator Example Output

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

 

Mathematics

Physics

Social Studies

Chemistry

Biology

History

Geography

English

Zoology

Botany

Civics

Drawing

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User