C# LINQ Enumerable.Select Method in ASP.Net

Updated on 10 Apr 2012,
Published on 25 Oct 2010

The Select method of System.Linq.Enumerable class enables to project the elements of a sequence into a new form. It implements a transform function as a selector that produces a new element corresponding to each element available in the provided sequence. You can generate an anonymous type of IEnumerable<T> sequence by applying projection criteria using selector transform function over the elements of the sequence using LINQ Select method. It has the following two types of overloaded functions which accept one or more parameters and generate the resulting sequence:


public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);

It accepts a single parameter that specifies a transform function selector and generates a new form for each element in a sequence.


public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);

It also accepts only single parameter that allows to create a selector having one additional parameter i.e. index. It enables to incorporate the index of each element to project the elements of a sequence into new form.

C# LINQ Select Method Example in ASP.Net

// Data Source Sequence
List<Employee> Employees = new List<Employee>()
    new Employee(1, "Terry", "Adams", 30, 5),
    new Employee(2, "Hugo", "Garcia", 27, 3),
    new Employee(3, "Fadi", "Fakhouri", 32, 5),
    new Employee(4, "Debra", "Garcia", 30, 4),
    new Employee(5, "Lance", "Tucker", 35, 7)

// Example 1:
IEnumerable<string> query1 = Employees.Select(employee => employee.FirstName + " " + employee.LastName);

foreach (string name in query1)
    Response.Output.Write("{0}<br />", name);

// Example 2:
IEnumerable<string> query2 = Employees.Select((employee, index) => string.Format("{0} - {1} {2}", index, employee.FirstName, employee.LastName));

foreach (string name in query2)
    Response.Output.Write("{0}<br />", name);

// Output:
// Example 1:
// Terry Adams
// Hugo Garcia
// Fadi Fakhouri
// Debra Garcia
// Lance Tucker
// Example 2:
// 0 - Terry Adams
// 1 - Hugo Garcia
// 2 - Fadi Fakhouri
// 3 - Debra Garcia
// 4 - Lance Tucker

C# Employee class code:

public class Employee
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public int Experience { get; set; }

    public Employee(int Id, string firstName, string lastName, int age, int experience)
        this.ID = Id;
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Age = age;
        this.Experience = experience;

In the above C# sample code we have created two examples to illustrate the use of LINQ Select method. In the first example we have used the first overloaded function of Select method to project the elements of the sequence into new form containing the string type elements. The second example shows the use of second overloaded function of LINQ Select method where we have used a selector function specified with element and index parameters that implements the transformation over each element of the sequence where index value can also be used.

Continue to next tutorial: C# LINQ Enumerable.SelectMany Method in ASP.Net to learn how to flatten the sub sequence elements into one sequence.

0 Responses to "C# LINQ Enumerable.Select Method in ASP.Net"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email