LINQ | Projection Operator | Select

In LINQ, projection is an operation which converts an object into the new form which holds only those properties that 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

Select Operator

The select operator returns the IEnumerator collection, which holds the items based on a transformation function. Or in other words, we can say that select operator is used when you want to select a single value from the given collection.



Select using Query Syntax: In LINQ, a Select operator is used in creating a query using query syntax. In the query expression, the expression ends with Select or GroupBy clause. In a query, the select operator is used to return the collection of the custom class or anonymous type that include the properties as per the need of the user and also used to manipulate the result according to the user need. Select operator support query syntax in both C# and VB.NET language As shown in the below examples:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the name 
// of 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;
    }
}
  
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 },
                new Employee() { emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000 },
                new Employee() { emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000 },
                new Employee() { emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000 },
                new Employee() { emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000 },
                new Employee() { emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000 },
        };
  
        // Query to find the name of the employee
        // Using select in  Query Syntax
        var res = from e in emp
                      select e.emp_name;
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name: {0}", val);
        }
    }
}

chevron_right


Output:

Employee Name: Anjita
Employee Name: Soniya
Employee Name: Rohit
Employee Name: Supriya
Employee Name: Anil
Employee Name: Anju

Select using Method Syntax: In method syntax, the select operator is optional, you may use it to shape the data. The Select method is present in both the Queryable and Enumerable class. It supports method syntax in both C# and VB.Net languages. The use of the select method is shown in the below examples.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the name 
// and id of 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;
    }
}
  
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 },
                new Employee() { emp_id = 210, emp_name = "Soniya", emp_gender = "Female", emp_hire_date = "22/4/2018", emp_salary = 30000 },
                new Employee() { emp_id = 211, emp_name = "Rohit", emp_gender = "Male", emp_hire_date = "3/5/2016", emp_salary = 40000 },
                new Employee() { emp_id = 212, emp_name = "Supriya", emp_gender = "Female", emp_hire_date = "4/8/2017", emp_salary = 40000 },
                new Employee() { emp_id = 213, emp_name = "Anil", emp_gender = "Male", emp_hire_date = "12/1/2016", emp_salary = 40000 },
                new Employee() { emp_id = 214, emp_name = "Anju", emp_gender = "Female", emp_hire_date = "17/6/2015", emp_salary = 50000 },
        };
  
        // finding the name and id of the 
        // employee Using select method
        var res = emp.Select(a => new { ID = a.emp_id, Name = a.emp_name });
  
        foreach(var val in res)
        {
            Console.WriteLine("Employee Name : {0}  Employee ID : {1}",
                                                     val.Name, val.ID);
        }
    }
}

chevron_right


Output:

Employee Name : Anjita  Employee ID : 209
Employee Name : Soniya  Employee ID : 210
Employee Name : Rohit  Employee ID : 211
Employee Name : Supriya  Employee ID : 212
Employee Name : Anil  Employee ID : 213
Employee Name : Anju  Employee ID : 214


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.