What is LINQ in C#?
LINQ stands for Language Integrated Query. It is a set of C# language features and .NET APIs that lets developers query, filter, transform, group, order, and aggregate data using a consistent syntax.
LINQ interview questions covering fundamentals, query and method syntax, IEnumerable, IQueryable, lambdas, predicates, projection and core query concepts.
Open a question for the full answer, code and interview guidance.
LINQ stands for Language Integrated Query. It is a set of C# language features and .NET APIs that lets developers query, filter, transform, group, order, and aggregate data using a consistent syntax.
LINQ provides strongly typed queries directly in C#, reduces repetitive looping and filtering code, supports IntelliSense and compile-time checking, and offers a consistent query model across different data sources.
LINQ can query any source that exposes the appropriate enumerable or queryable interfaces. Common examples include arrays, List<T>, dictionaries, other IEnumerable<T> collections, Entity Framework Core DbSet<T>, XML, and custom query providers.
LINQ to Objects refers to using LINQ operators against in-memory collections that implement IEnumerable<T>. The query logic executes as .NET code in the application process.
Query syntax is the SQL-like C# syntax that uses keywords such as from, where, orderby, select, group, and join. The compiler translates supported query expressions into calls to standard LINQ methods.
Method syntax uses extension methods such as Where, Select, OrderBy, GroupBy, Join, Any, and FirstOrDefault. It is also called fluent syntax because operators can be chained together.
Query syntax uses C# query keywords, while method syntax calls LINQ extension methods directly. Many query-syntax expressions are translated by the compiler into the corresponding method calls.
IEnumerable<T> represents a sequence that can be enumerated one element at a time. It is the fundamental generic interface used by LINQ to Objects.
IQueryable<T> represents a query that can be interpreted by a query provider. In addition to being enumerable, it exposes an expression tree describing the query.
IEnumerable<T> is primarily used for in-memory enumeration, and its LINQ operators execute as .NET code. IQueryable<T> carries an expression tree that a provider can inspect and translate.
The non-generic IEnumerable interface represents an object that can provide an enumerator. Generic IEnumerable<T> adds strong typing and is preferred for modern C# collections.
IEnumerator<T> is the object that performs iteration over an IEnumerable<T> sequence. MoveNext advances to the next element, and Current exposes the current value.
LINQ operators such as Where, Select, OrderBy, and Any are implemented as extension methods. Extension methods allow static methods to be called with instance-method syntax when the target type is compatible with the method's first parameter.
A lambda expression is a concise anonymous function commonly passed to LINQ operators. It can describe predicates, projections, key selectors, ordering rules, and other query behavior.
A predicate is a function that evaluates an element and returns true or false. LINQ operators such as Where, Any, All, First, and Count can use predicates to decide which elements satisfy a condition.
Projection transforms each source element into another shape or value. Select is the primary projection operator, while SelectMany can flatten nested sequences.
An anonymous type is a compiler-generated type created with the new { ... } syntax without explicitly declaring a class. It is useful for local projections where a temporary strongly typed shape is sufficient.
Standard query operators are the LINQ methods that perform operations on sequences. Major categories include filtering, projection, ordering, grouping, joining, set operations, element selection, aggregation, quantifiers, partitioning, and conversion.
Where filters a sequence by returning only elements that satisfy a predicate. It does not normally modify the original source.
Select projects every source element into a new value or shape. The output type can be the same as the input type or completely different.
SelectMany projects each source element to a sequence and then flattens all resulting sequences into one sequence. It is useful for nested collections such as departments containing employees or orders containing order lines.
Deferred execution means constructing a LINQ query does not necessarily execute it immediately. Many operators such as Where and Select return an object that represents the query, and the source is processed later when the result is enumerated.
Immediate execution occurs when an operator must produce a result now rather than return a deferred sequence. Materialization methods such as ToList and ToArray enumerate the source immediately, and scalar operators such as Count, First, Any, and Sum also execute to produce their result.
Materialization means executing a query and storing its results in a concrete in-memory representation, commonly with ToList, ToArray, ToDictionary, or similar conversion operators.
The execution location affects performance, supported operations, memory usage, network traffic, and even query semantics. An IEnumerable<T> query normally runs in application code, while an IQueryable<T> query may be translated and executed by a remote provider such as a database.