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:
- Select
- 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:
using System;
using System.Linq;
using System.Collections.Generic;
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 {
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 },
};
var res = from e in emp
select e.emp_name;
foreach ( var val in res)
{
Console.WriteLine( "Employee Name: {0}" , val);
}
}
}
|
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:
using System;
using System.Linq;
using System.Collections.Generic;
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 {
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 },
};
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);
}
}
}
|
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 May, 2019
Like Article
Save Article