Glossary / LINQ

LINQ

Language Integrated Query (LINQ) provides query operators for filtering, projecting, grouping, ordering, joining, and aggregating data. C# query syntax and method syntax compile to calls on LINQ APIs or providers; behavior depends on whether the source is an in-memory sequence, asynchronous stream, or provider such as a database.

Enumerable and Queryable

IEnumerable<T> operators such as Enumerable.Where use delegates and execute .NET code, usually with deferred execution. IQueryable<T> operators build expression trees that a provider translates, often to SQL. A lambda can bind as a delegate or expression tree based on overload resolution. Calling AsEnumerable() moves subsequent operators to client-side execution; ToList() materializes and can cause large data transfer/memory use.

var active = customers.Where(c => c.IsActive).Select(c => c.Name);

Most sequence operators are deferred until enumeration; aggregations like Count, First, and Sum execute immediately. Re-enumerating a deferred query may repeat work or observe changed source data. Query providers differ in supported operators and semantics such as case sensitivity, null, and ordering.

Engineering guidance

Keep filtering/projection server-side when appropriate, project only needed columns, and avoid accidental materialization or N+1 queries. Inspect generated SQL, query plans, and enumeration counts. Use async provider APIs for I/O-bound database access, while remembering LINQ itself does not make synchronous work asynchronous.

Interview questions

  1. IEnumerable vs IQueryable? In-memory/delegate iteration vs expression-tree provider translation.
  2. What is deferred execution? Query work starts when enumerated, not necessarily when constructed.
  3. What does ToList() do? Executes and materializes the sequence immediately.
  4. Why can a LINQ query run twice? Enumeration may repeat deferred source and operator work.
  5. What is AsEnumerable() effect? Subsequent operators bind to Enumerable and execute client-side.

References