Difference between Select and SelectMany in LINQ

By | February 1, 2018

Select is used to select value from a collection whereas SelectMany is used to select values from a collection of collection i.e. nested collection.

Select and SelectMany both are projection operators. In below example we will return a list of courses, each containing a list of subjects. You will see that Select will return a list of lists of subjects while SelectMany will flatten the lists into a single list of subjects.

Example:-

using System;
using System.Linq;
using System.Collections.Generic;
class Course
{
public int courseId;
public string courseName;
public List<string> subjects;
}
public class Program
{
public static void Main(string[] args)
{

List<Course> courses = new List<Course> {
new Course {
courseId = 1,
courseName = "BCA",
subjects = new List<string> { "Computer Science", "Data Structure", "Math" }
},
new Course {
courseId = 2,
courseName = "BBA",
subjects = new List<string> { "English", "Economics", "Business" }
},
new Course {
courseId = 3,
courseName = "BA",
subjects = new List<string> { "English", "Physical Science", "History" }
}
};
// Query using Select()
var subjects = courses.Select(c => c.subjects);
foreach (var sub in subjects)
{
Console.WriteLine(sub.ToString());
}
}
}

Output:
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]
System.Collections.Generic.List`1[System.String]

In above program, it returns the collection of collections so, we need to iterate over each list of subjects to achieve the desired results. Lets try it in below example.

public class Program
{
public static void Main(string[] args)
{

List<Course> courses = new List<Course> {
new Course {
courseId = 1,
courseName = "BCA",
subjects = new List<string> { "Computer Science", "Data Structure", "Math" }
},
new Course {
courseId = 2,
courseName = "BBA",
subjects = new List<string> { "English", "Economics", "Business" }
},
new Course {
courseId = 3,
courseName = "BA",
subjects = new List<string> { "English", "Physical Science", "History" }
}
};
var subjects = courses.Select(c => c.subjects);
foreach (var sub in subjects)
{
// We add one more foreach loops to iterate through because it returns a collection of arrays.
foreach (var x in sub)
{
Console.WriteLine(x.ToString());
}
Console.WriteLine();
}
}
}

Output:

Computer Science
Data Structure
Math

English
Economics
Business

English
Physical Science
History

Now, we are getting the desire output. But we can achieve the same result with the help of SelectMany() operator using single foreach loop because SelectMany will flatten the list into a single result. Lets take an example with SelectMany operator.

public class Program
{
public static void Main(string[] args)
{
List<Course> courses = new List<Course> {
new Course {
courseId = 1,
courseName = "BCA",
subjects = new List<string> { "Computer Science", "Data Structure", "Math" }
},
new Course {
courseId = 2,
courseName = "BBA",
subjects = new List<string> { "English", "Economics", "Business" }
},
new Course {
courseId = 3,
courseName = "BA",
subjects = new List<string> { "English", "Physical Science", "History" }
}
};

// Query using SelectMany()
var subjects = courses.SelectMany(c => c.subjects);
foreach (var sub in subjects)
{
Console.WriteLine(sub.ToString());
}
}
}

Output:

Computer Science
Data Structure
Math
English
Economics
Business
English
Physical Science
History

 

I hope you will enjoy the difference between Select and SelectMany in LINQ. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Like it? Share it