Skip to content

C# LINQ Select — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# LINQ Select. We cover key concepts, practical examples, and best practices.

You have a list of objects and need a list of a single property. You write a foreach loop that extracts values into a new list. The LINQ Select method projects each element through a transform function, returning the transformed sequence.

Wrong

var names = new List<string>();
foreach (var user in users)
{
    names.Add(user.Name);
}

Output: Works. Five lines for a simple projection.

var names = users.Select(u => u.Name).ToList();

Output: Same list, one line. The lambda u => u.Name projects each user to their name.

Select also provides the index in the transform:

var indexed = users.Select((u, i) => $"{i + 1}. {u.Name}").ToList();
// ["1. Alice", "2. Bob", "3. Charlie"]

Chaining Select with other LINQ methods:

var result = users
    .Where(u => u.IsActive)
    .Select(u => new { u.Name, u.Email })
    .ToList();

Prevention

  • Use Select instead of foreach + new list for projections.
  • Use .Select(x => x.Property) for extracting a single member.
  • Use .Select(x => new { x.A, x.B }) for anonymous type projections.
  • Use .Select((x, index) => ...) when you need the element index.
  • Use SelectMany when each element produces multiple output elements.
  • Remember Select is lazy — materialize with .ToList() or .ToArray().

Common Mistakes with linq select

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Does Select modify the original collection?

No. LINQ methods never modify the source collection. Select returns a new IEnumerable<T> that lazily applies the transform. The source collection remains unchanged.

What is the difference between Select and Cast?

Select applies an arbitrary transform: .Select(x => (TargetType)x). Cast<TargetType> does the same cast but without a lambda. Use Cast when every element must be cast to the same type; use Select for any other transform.

Can I use Select with IQueryable for database queries?

Yes. Select on IQueryable<T> translates to SQL SELECT clause. The projection becomes part of the SQL query, so only the selected columns are transferred. This is crucial for performance — never select all columns when you only need one.

Select is used in Doda Browser to transform raw API responses into view models. For more LINQ, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro