Friday, 15 April 2016

Difference between Select and SelectMany in LINQ



Select and SelectMany are projection operators.
Select operator is used to select value from a collection and SelectMany operator is used to select values from a collection of collection i.e. nested collection.
Select operator produce one result value for every source value while SelectMany produce a single result that contains a concatenated value for every source value. Actually, SelectMany operator flatten IEnumerable<IEnumerable<T>> to IEnumrable<T> i.e. list of list to list.
class Employee
{
    public string Name { get; set; }
    public List<string> Skills { get; set; }
}

public void TestSelectAndSelectMany()
{
    List<Employee> Employees = new List<Employee>();

    Employee Emp1 = new Employee { Name = "Ravi", Skills = new List<string> { "C", "C++", "Java" } };

    Employee Emp2 = new Employee { Name = "Minu", Skills = new List<string> { "SQL Server", "C#", "ASP.NET" } };

    Employee Emp3 = new Employee { Name = "Aditi", Skills = new List<string> { "C#", "ASP.NET MVC", "Windows Azure", "SQL Server" } };

    Employees.Add(Emp1);
    Employees.Add(Emp2);
    Employees.Add(Emp3);

    //-- Query using Select()
    IEnumerable<List<String>> ResultSelect = Employees.Select(e => e.Skills);

    Console.WriteLine("**************** Select ******************");

    //-- Two foreach loops are required to iterate through the results
    //-- because the query returns a collection of arrays.
    foreach (List<String> SkillList in ResultSelect)
    {
        foreach (string Skill in SkillList)
        {
            Console.WriteLine(Skill);
        }
        Console.WriteLine();
    }

    //-- Query using SelectMany()
    IEnumerable<string> ResultSelectMany = Employees.SelectMany(emp => emp.Skills);

    Console.WriteLine("**************** SelectMany ******************");

    //-- Only one foreach loop is required to iterate through the results
    //-- since query returns a one-dimensional collection.
    foreach (string skill in ResultSelectMany)
    {
        Console.WriteLine(skill);
    }
}

Output:

**************** Select ******************
C
C++
Java

SQL Server
C#
ASP.NET

C#
ASP.NET MVC
Windows Azure
SQL Server

**************** SelectMany ****************
C
C++
Java
SQL Server
C#
ASP.NET
C#
ASP.NET MVC
Windows Azure
SQL Server



4 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete
  2. ok thanks for this post it's quite informative and I have learned new things.

    kajal agarwal hot

    ReplyDelete