Multiple Enumerations in LINQ Expressions

Published: (December 15, 2025 at 01:00 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Why Multiple Enumeration Is a Problem

  • Repeated work – Each enumeration creates a new enumerator that starts from the beginning of the sequence.
  • Resource consumption – For queries that hit a database, call a web service, or read a file, each enumeration repeats the costly operation.
  • Unintended side effects – Some enumerators may have stateful side effects (e.g., logging, counters) that run multiple times.

Materializing Early

To avoid repeated enumeration, materialize the sequence as soon as you know you’ll need to traverse it more than once. Materialization converts the lazy IEnumerable into a concrete collection (List, T[], etc.) that can be reused without re‑executing the underlying query.

// Lazy sequence
IEnumerable numbers = Enumerable.Range(1, 10);

// This will enumerate the sequence twice
foreach (int n in numbers) Console.WriteLine(n);
foreach (int n in numbers) Console.WriteLine(n);

Materialize with ToList() (or ToArray())

IEnumerable numbers = Enumerable.Range(1, 10);
List materializedNumbers = numbers.ToList();   // materialized once

// Both loops now use the same in‑memory list
foreach (int n in materializedNumbers) Console.WriteLine(n);
foreach (int n in materializedNumbers) Console.WriteLine(n);

Best Practices

RecommendationReason
Materialize early when you know the collection will be iterated multiple times.Guarantees a single enumeration of the original source.
Use ToList() or ToArray() to convert IEnumerable to a concrete type.Provides fast indexed access and avoids repeated query execution.
Document method parameters that will be enumerated more than once.Alerts callers to potential performance costs.
Profile your code to locate hot spots where the same IEnumerable is enumerated repeatedly.Helps you focus optimization effort where it matters most.
Understand deferred execution (see Part 1) to predict when enumeration occurs.Prevents accidental multiple passes over a lazy query.

Scenarios Where Multiple Enumeration Is Especially Harmful

  • Database queries – Each enumeration may re‑execute the SQL command, causing multiple round‑trips.
  • External API calls – Re‑enumerating can trigger repeated network requests.
  • File I/O – Reading a file multiple times wastes I/O bandwidth.
  • Expensive computations – Re‑calculating complex transformations degrades performance.

Quick Checklist

  • Do I need to iterate the collection more than once?
  • If yes, call ToList()/ToArray() once and reuse the result.
  • Have I documented any parameters that will be enumerated multiple times?
  • Have I profiled the code to ensure no hidden multiple enumerations remain?

Further Reading

Back to Blog

Related posts

Read more »

Domina el uso de paquetes NuGet en .NET

¿Qué es realmente NuGet? Imagina que NuGet es el Amazon o Mercado Libre de .NET. Tú no fabricas cada tornillo de tu mueble; los pides a la tienda. - El Package...