Home > DeveloperSection > Forums > Use Select Operator in linq.
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    March-20-2016 10:38 PM

 LINQ LINQ 
Ratings:


 1 Reply(s)
 212  View(s)
Rate this:
We want to use Select Operator in linq. How to do this please help me.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    March-20-2016 10:41 PM

The Select operator always returns an IEnumerable collection which contains elements based on a transformation function. It is similar to the Select clause of SQL that produces a flat result set.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Prototype

{

class Example

{

static void Main()

{

IList<Employee> empList = new List<Employee>() {

new Employee() { ID = 1, EmpName = "Rohit", Age = 13 } ,

new Employee() { ID = 2, EmpName = "Monu", Age = 21 } ,

new Employee() { ID = 3, EmpName = "Mohsan", Age = 18 } ,

new Employee() { ID = 4, EmpName = "Sohel" , Age = 20 } ,

new Employee() { ID = 5, EmpName = "Arun" , Age = 20 } ,

new Employee() { ID = 6, EmpName = "Karan" , Age = 25 }

};

var selectResult = from s in empList

select new { Name = "Mr. " + s.EmpName, Age = s.Age };

foreach (var item in selectResult)

Console.WriteLine("Student Name: {0}, Age: {1}", item.Name, item.Age);

Console.ReadLine();

}

}

class Employee

{

public int ID { get; set; }

public string EmpName { get; set; }

public int Age { get; set; }

}

}


Don't want to miss updates? Please click the below button!

Follow MindStick