Barış Kısır

Senior Software Developer

Navigation
 » Home
 » RSS

LINQ Select vs SelectMany

25 Oct 2016 » csharp

The difference between select and selectmany is

Select gets a list of lists of X objects

SelectMany gets a list of X objects

Let me give you an example

public class Course
{
	public string CourseName { get; set; }
}
public class Student
{
public string Name { get; set; }
public List<Course> Courses { get; set; }
public Student()
	{
	this.Courses = new List<Course>();
	}
}
var studentList = GenerateRandomStudentList();
// SelectMany returns List<Course>
List<Course> selectManyCourses = studentList.SelectMany(x => x.Courses).ToList();
// Select returns List<List<Course>>
List<List<Course>> selectCourses = studentList.Select(x => x.Courses).ToList();

You can download source code from here –> Download

Select

SelectMany