What is a join in LINQ?
A join combines related elements from two data sources by matching keys. LINQ provides the Join operator for an inner join and GroupJoin for grouped relationships.
LINQ interview questions covering joins, GroupJoin, left joins, grouping, lookups, set operators, Any, All, Contains and equality semantics.
Open a question for the full answer, code and interview guidance.
A join combines related elements from two data sources by matching keys. LINQ provides the Join operator for an inner join and GroupJoin for grouped relationships.
The join clause in query syntax performs an inner join by matching a key from the first source with a key from the second source. Only matching pairs are returned.
Use the Join extension method. It receives the inner sequence, an outer key selector, an inner key selector, and a result selector.
GroupJoin correlates each element from the outer sequence with a collection of matching elements from the inner sequence. It is conceptually similar to a grouped relational join.
Join produces one result for each matching pair. If a customer has three orders, an inner Join can produce three rows for that customer.
A common LINQ left-join pattern uses GroupJoin followed by SelectMany and DefaultIfEmpty. Query syntax expresses the same pattern with join ... into and a second from clause using DefaultIfEmpty.
DefaultIfEmpty returns the source sequence when it contains elements. If the source is empty, it returns a sequence containing one default value.
Create matching composite keys on both sides of the join. Anonymous types are commonly used for this purpose, and the corresponding property names and compatible types must match.
GroupBy partitions a sequence into groups according to a key selector. Each resulting group has a Key and contains the elements associated with that key.
After GroupBy, project each group with Select and call aggregate operators such as Count, Sum, Average, Min, or Max on that group.
Use a composite key, commonly an anonymous type, as the GroupBy key. Each distinct combination becomes a separate group.
GroupBy commonly returns a deferred sequence of groups. ToLookup immediately enumerates the source and creates a lookup structure that maps keys to one or more values.
A Lookup<TKey,TElement> is a one-to-many key-based collection produced by ToLookup. Unlike Dictionary<TKey,TValue>, each key can map to multiple elements.
Set operators combine or compare sequences using set-like semantics. Common operators include Distinct, Union, Intersect, Except, and their key-selector variants available in modern .NET.
Distinct removes duplicate elements according to the applicable equality comparison. For primitive values this is usually straightforward.
DistinctBy returns the first element for each distinct key selected from the source. It is useful when objects should be deduplicated by a property rather than by their complete equality definition.
Union returns the distinct elements that appear in either of two sequences. Duplicate values are removed according to the equality comparer.
Union combines two sequences and removes duplicate elements according to equality semantics. Concat appends the second sequence after the first and preserves duplicates.
Intersect returns distinct elements that appear in both sequences. Equality comparison determines whether elements match.
Except returns distinct elements from the first sequence that do not appear in the second sequence. It is a set-difference operation.
Any determines whether a sequence contains at least one element. With a predicate, it determines whether at least one element satisfies the condition.
All returns true when every element satisfies a predicate. It stops when it finds an element that fails the condition.
Any expresses an existence check directly and can stop after finding the first matching element. Count calculates how many matching elements exist and may require more work.
Contains determines whether a sequence contains a specified value according to its equality semantics. It is useful for membership checks.
Many LINQ operations depend on equality. Join and GroupJoin match keys, GroupBy identifies group keys, and operators such as Distinct, Union, Intersect, Except, and Contains determine whether values are equal.