LINQ | Projection Operator | SelectMany

In LINQ, projection is an operation which converts an object into the new form which holds only those properties which will be subsequently used. By using projection, a developer can create a new type which is built from each object. You are allowed to project property and conduct mathematical function on it, and you can also project the original object without transforming it.

In LINQ, the following projection operations are available:

  1. Select
  2. SelectMany

SelectMany Operator

The SelectMany operator returns sequences of values which are based on the transformation function and then make them into one sequence. Or in other words, we can say, SelectMany operator is used when you want to select values from the multiple collections or if you want a result from the list of the lists and wants to display into a single sequence.



SelectMany in Query Syntax: In Query Syntax, the working of SelectMany operator is achieved by using multiple from clause. As shown in the below example.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the languages
// known by the employee
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
    public string emp_name
    {
        get;
        set;
    }
    public string emp_gender
    {
        get;
        set;
    }
    public string emp_hire_date
    {
        get;
        set;
    }
    public int emp_salary
    {
        get;
        set;
    }
    public List<string> emp_lang
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female", emp_hire_date = "12/3/2017", emp_salary = 20000, emp_lang = new List<string>{"C#", "VB"} },
                new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000,  emp_lang = new List<string>{ "Java"} },
                new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000,  emp_lang = new List<string>{ "C++", "SQL"} },
                new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000,  emp_lang = new List<string>{"Python", "C", "PHP"} },
                new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000,  emp_lang = new List<string>{"HTML", "JQuery"} },
                new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000,  emp_lang = new List<string>{"JavaScript", "Perl"} },
        };
  
        // Query to find the languages
        // known by the employee
        var res = from e in emp
                      from e2 in e.emp_lang
                          select e2;
  
        Console.WriteLine("Languages known by all the employees are:");
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}

chevron_right


Output:

Languages known by all the employees are:
C#
VB
Java
C++
SQL
Python
C
PHP
HTML
JQuery
JavaScript
Perl

SelectMany in Method Syntax: The SelectMany method is present in both the Queryable and Enumerable class and supported by both C# and VB.Net languages.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the languages
// known by the employee
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
    public string emp_name
    {
        get;
        set;
    }
    public string emp_gender
    {
        get;
        set;
    }
    public string emp_hire_date
    {
        get;
        set;
    }
    public int emp_salary
    {
        get;
        set;
    }
    public List<string> emp_lang
    {
        get;
        set;
    }
}
  
public class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female", emp_hire_date = "12/3/2017", emp_salary = 20000, emp_lang = new List<string>{"C#", "VB"} },
                new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000,  emp_lang = new List<string>{ "Java"} },
                new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000,  emp_lang = new List<string>{ "C++", "SQL"} },
                new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000,  emp_lang = new List<string>{"Python", "C", "PHP"} },
                new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000,  emp_lang = new List<string>{"HTML", "JQuery"} },
                new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000,  emp_lang = new List<string>{"JavaScript", "Perl"} },
        };
  
        // Finding the languages known by the employee
        // Using SelectMany method
        var res = emp.SelectMany(a => a.emp_lang);
        Console.WriteLine("Languages known by all the employees are:");
        foreach(var val in res)
        {
            Console.WriteLine(val);
        }
    }
}

chevron_right


Output:

Languages known by all the employees are:
C#
VB
Java
C++
SQL
Python
C
PHP
HTML
JQuery
JavaScript
Perl


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.