LINQ Interview

Joins, Grouping, Set & Quantifier Operators

LINQ interview questions covering joins, GroupJoin, left joins, grouping, lookups, set operators, Any, All, Contains and equality semantics.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
51
Mid-LevelIntermediate

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.

52
Mid-LevelIntermediate

How do you write an inner join using LINQ query syntax?

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.

53
Mid-LevelIntermediate

How do you perform an inner join using method syntax?

Use the Join extension method. It receives the inner sequence, an outer key selector, an inner key selector, and a result selector.

54
Mid-LevelIntermediate

What is GroupJoin in LINQ?

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.

55
Mid-LevelIntermediate

What is the difference between Join and GroupJoin?

Join produces one result for each matching pair. If a customer has three orders, an inner Join can produce three rows for that customer.

56
Mid-LevelIntermediate

How do you perform a left outer join in LINQ?

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.

57
Mid-LevelIntermediate

What does DefaultIfEmpty do in LINQ?

DefaultIfEmpty returns the source sequence when it contains elements. If the source is empty, it returns a sequence containing one default value.

58
Mid-LevelIntermediate

How can you join on multiple columns in LINQ?

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.

59
Mid-LevelIntermediate

What is GroupBy in LINQ?

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.

60
Mid-LevelIntermediate

How do you calculate aggregates for each LINQ group?

After GroupBy, project each group with Select and call aggregate operators such as Count, Sum, Average, Min, or Max on that group.

61
Mid-LevelIntermediate

How do you group by multiple fields in LINQ?

Use a composite key, commonly an anonymous type, as the GroupBy key. Each distinct combination becomes a separate group.

62
Mid-LevelIntermediate

What is the difference between GroupBy and ToLookup?

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.

63
Mid-LevelIntermediate

What is a Lookup<TKey,TElement>?

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.

64
Mid-LevelIntermediate

What are set operators in LINQ?

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.

65
Mid-LevelIntermediate

What does Distinct do in LINQ?

Distinct removes duplicate elements according to the applicable equality comparison. For primitive values this is usually straightforward.

66
Mid-LevelIntermediate

What is DistinctBy in LINQ?

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.

67
Mid-LevelIntermediate

What does Union do in LINQ?

Union returns the distinct elements that appear in either of two sequences. Duplicate values are removed according to the equality comparer.

68
Mid-LevelIntermediate

What is the difference between Union and Concat?

Union combines two sequences and removes duplicate elements according to equality semantics. Concat appends the second sequence after the first and preserves duplicates.

69
Mid-LevelIntermediate

What does Intersect do in LINQ?

Intersect returns distinct elements that appear in both sequences. Equality comparison determines whether elements match.

70
Mid-LevelIntermediate

What does Except do in LINQ?

Except returns distinct elements from the first sequence that do not appear in the second sequence. It is a set-difference operation.

71
Mid-LevelIntermediate

What does Any do in LINQ?

Any determines whether a sequence contains at least one element. With a predicate, it determines whether at least one element satisfies the condition.

72
Mid-LevelIntermediate

What does All do in LINQ?

All returns true when every element satisfies a predicate. It stops when it finds an element that fails the condition.

73
Mid-LevelIntermediate

What is the difference between Any and Count for existence checks?

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.

74
Mid-LevelIntermediate

What does Contains do in LINQ?

Contains determines whether a sequence contains a specified value according to its equality semantics. It is useful for membership checks.

75
Mid-LevelIntermediate

How do equality comparers affect joins, grouping, and set operations in LINQ?

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.