Web
Analytics
Projection Operators in LINQ | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Projection Operators in LINQ

Input sequenceà Output sequence (Input sequence of elements can be modified using projection operators).We can set output sequence format using projection operator.

  1. Select : Using select projection operator , we can project specified value from a collection.

Example: We have a user defined employeedetails

 

public class employeedetails

{

public string employeename;

public string department;

public int age;

}

Let’s create employeedetails list using collection.

employeedetails[] emp =

{

new employeedetails{ employeename="Ganesha", department="IT", age=30},

new employeedetails{ employeename="Yogesh", department="IT", age=20},

new employeedetails{ employeename="Ashish", department="Sales", age=10},

new employeedetails{ employeename="Pooja", department="Teaching", age=15},

new employeedetails{ employeename="Arti", department="Teaching", age=40},

};

To get the employee names or want to get output sequence in string

IEnumerable empname = emp.Select(x=>x.employeename);

To get the employee Age or want to get output sequence in int

IEnumerable empage = emp.Select(x => x.age);

 

Complete Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace LINQExample

{

class Program

{

static void Main(string[] args)

{

employeedetails[] emp =

{

new employeedetails{ employeename="Ganesha", department="IT", age=30},

new employeedetails{ employeename="Yogesh", department="IT", age=20},

new employeedetails{ employeename="Ashish", department="Sales", age=10},

new employeedetails{ employeename="Pooja", department="Teaching", age=15},

new employeedetails{ employeename="Arti", department="Teaching", age=40},

};

IEnumerable empname = emp.Select(x => x.employeename);

 

IEnumerable empage = emp.Select(x => x.age);

foreach (string empsname in empname)

{

Console.WriteLine("EmployeeName =" + empsname);

}

foreach (int empsage in empage)

{

Console.WriteLine("EmployeeName =" + empsage);

}

Console.ReadLine();

}

}

public class employeedetails

{

public string employeename;

public string department;

public int age;

}

}

 Program