0Pricing
C# Academy · Lesson

IEnumerable<T> pipelines; deferred execution

Build LINQ pipelines over IEnumerable and see deferred execution vs materialization with ToList/ToArray.

LINQ pipeline idea

Pipeline = chain of operators that transforms a sequence.

  • Source: an IEnumerable<T>
  • Operators: Where, Select, etc.
  • Sink: enumeration (foreach) or materialization (ToList)

Where + Select

Create a chain with Where then Select. The query runs as you iterate.

using System;
using System.Linq;

public class Program
{
  public static void Main(string[] args)
  {
    int[] numbers = new int[] { 1, 2, 3, 4, 5 };

    // Build pipeline: keep even numbers, then square them
    var query = numbers
      .Where(n => n % 2 == 0)
      .Select(n => n * n);

    foreach (int x in query)
    {
      Console.WriteLine(x); // 4, 16
    }
  }
}

All lessons in this course

  1. IEnumerable pipelines; deferred execution
  2. Select, Where, OrderBy, Take/Skip
  3. Query vs method syntax
← Back to C# Academy