C# Linq Select Method

The Select method in C# LINQ is used to transform each element of a collection into a new form. It projects each element of a sequence into a new sequence by applying a transformation function to each element.

Syntax

Following is the basic syntax for the Select method −

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

The overload with index parameter −

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

Parameters

  • source − The input sequence to transform

  • selector − A transform function to apply to each element

  • int − The zero-based index of the element (in overloaded version)

Return Value

Returns an IEnumerable<TResult> whose elements are the result of invoking the transform function on each element of the source sequence.

Using Select for Simple Transformations

Example

using System;
using System.Linq;

public class Demo {
   public static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };
      var squares = numbers.Select(x => x * x);
      
      Console.WriteLine("Original numbers:");
      foreach (var num in numbers) {
         Console.Write(num + " ");
      }
      
      Console.WriteLine("\nSquared numbers:");
      foreach (var square in squares) {
         Console.Write(square + " ");
      }
   }
}

The output of the above code is −

Original numbers:
1 2 3 4 5 
Squared numbers:
1 4 9 16 25 

Using Select with Index

Example

using System;
using System.Linq;

public class Demo {
   public static void Main() {
      string[] stationery = { "diary", "board", "pencil", "whiteboard" };
      var res = stationery.Select((item, index) => new { 
         Index = index, 
         Original = item,
         Result = item.Substring(0, Math.Min(index + 4, item.Length)) 
      });
      
      foreach (var str in res) {
         Console.WriteLine($"Index: {str.Index}, Original: {str.Original}, Result: {str.Result}");
      }
   }
}

The output of the above code is −

Index: 0, Original: diary, Original: diary, Result: diar
Index: 1, Original: board, Original: board, Result: board
Index: 2, Original: pencil, Original: pencil, Result: pencil
Index: 3, Original: whiteboard, Original: whiteboard, Result: whitebo

Using Select with Complex Objects

Example

using System;
using System.Linq;
using System.Collections.Generic;

public class Student {
   public string Name { get; set; }
   public int Age { get; set; }
}

public class Demo {
   public static void Main() {
      var students = new List<Student> {
         new Student { Name = "Alice", Age = 20 },
         new Student { Name = "Bob", Age = 22 },
         new Student { Name = "Charlie", Age = 19 }
      };
      
      var studentNames = students.Select(s => s.Name.ToUpper());
      var studentInfo = students.Select(s => $"{s.Name} is {s.Age} years old");
      
      Console.WriteLine("Student Names (Uppercase):");
      foreach (var name in studentNames) {
         Console.WriteLine(name);
      }
      
      Console.WriteLine("\nStudent Information:");
      foreach (var info in studentInfo) {
         Console.WriteLine(info);
      }
   }
}

The output of the above code is −

Student Names (Uppercase):
ALICE
BOB
CHARLIE

Student Information:
Alice is 20 years old
Bob is 22 years old
Charlie is 19 years old

Common Use Cases

  • Data Transformation − Converting data from one type to another

  • Property Extraction − Selecting specific properties from complex objects

  • Mathematical Operations − Applying calculations to numeric collections

  • String Manipulation − Transforming string data (case changes, formatting, etc.)

Conclusion

The LINQ Select method is essential for transforming collections in C#. It allows you to project each element into a new form using lambda expressions, making data transformation operations clean and efficient. The method supports both simple transformations and complex projections with index-based operations.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements