Use Select Operator in linq.

We want to use Select Operator in linq. How to do this please help me.
Last updated:3/20/2016 10:41:15 PM

1 Answers

Aditya Patel
Aditya Patel

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; }

}

}

Answer