LINQ Interview

Filtering, Projection, Ordering & Element Operators

LINQ interview questions covering filtering, Select, SelectMany, ordering, sorting and element operators such as First, Single, Last and ElementAt.

25 Questions Filter by level

Interview questions

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

25 Results
26
JuniorBeginner

What is filtering in LINQ?

Filtering selects only the elements that satisfy a condition. The primary LINQ filtering operator is Where.

27
JuniorBeginner

Can Where use multiple conditions?

Yes. A single Where predicate can combine conditions with Boolean operators such as && and ||. Multiple Where calls can also be chained.

28
JuniorBeginner

What is the indexed overload of Where?

For IEnumerable<T>, Where has an overload whose predicate receives both the element and its zero-based index. This allows filtering based on the element's position.

29
JuniorBeginner

What is projection with Select?

Projection transforms each source element into a desired output shape. Select applies a selector to every element and returns the projected sequence.

30
JuniorBeginner

Can Select change the type of a LINQ sequence?

Yes. Select can transform a sequence from one element type to another. For example, IEnumerable<Product> can become IEnumerable<string>, IEnumerable<decimal>, or IEnumerable<ProductDto>.

31
JuniorBeginner

What is the indexed overload of Select?

For IEnumerable<T>, Select can provide both the current element and its zero-based index to the selector. This is useful when the projected result needs a sequence number or position.

32
JuniorBeginner

What is the difference between Select and SelectMany?

Select maps each source element to one result. If that result is itself a collection, Select produces a sequence of collections. SelectMany maps each source element to a sequence and then flattens all those sequences into one sequence.

33
JuniorBeginner

How can SelectMany preserve information from the parent object?

SelectMany has overloads that support a result selector, or the projection can create an object containing values from both the parent and child.

34
JuniorBeginner

What does OrderBy do in LINQ?

OrderBy sorts a sequence in ascending order according to a key selector. For strings this typically means ascending text order, and for numeric or date values it means lowest or earliest first according to the comparer.

35
JuniorBeginner

What does OrderByDescending do?

OrderByDescending sorts a sequence in descending order by the selected key. It is useful for scenarios such as newest records first, highest prices first, or highest scores first.

36
JuniorBeginner

What is the difference between ThenBy and another OrderBy?

ThenBy adds a secondary ascending sort to an existing ordering. Another OrderBy starts a new primary ordering and can replace the significance of the previous ordering.

37
JuniorBeginner

What does ThenByDescending do?

ThenByDescending adds a secondary or later descending ordering to an existing ordered sequence. It is used after OrderBy, OrderByDescending, ThenBy, or another compatible ordered operation.

38
JuniorBeginner

How do you sort by multiple fields in LINQ?

Start with OrderBy or OrderByDescending for the primary key and add ThenBy or ThenByDescending for subsequent keys.

39
JuniorBeginner

What does Reverse do in LINQ?

Reverse returns the elements of a sequence in the opposite order from their current enumeration order. It does not mean the same thing as sorting descending unless the sequence is already sorted in a meaningful ascending order.

40
JuniorBeginner

What does First do in LINQ?

First returns the first element of a sequence. An overload can return the first element satisfying a predicate.

41
Mid-LevelIntermediate

What does FirstOrDefault do?

FirstOrDefault returns the first element, or the default value when the sequence contains no matching element. For reference types the default is normally null; for non-nullable value types it is the type's default value.

42
Mid-LevelIntermediate

What is the difference between First and FirstOrDefault?

First throws an exception when no element exists, while FirstOrDefault returns the default value. The choice should reflect the semantics of the application.

43
Mid-LevelIntermediate

What does Single do in LINQ?

Single returns the only element in a sequence and throws if the sequence contains zero elements or more than one element. The predicate overload similarly requires exactly one match.

44
Mid-LevelIntermediate

What does SingleOrDefault do?

SingleOrDefault returns the only matching element or the default value when no element exists, but it still throws if more than one element matches.

45
Mid-LevelIntermediate

What is the difference between FirstOrDefault and SingleOrDefault?

FirstOrDefault returns the first matching element and does not complain if additional matches exist. SingleOrDefault requires at most one matching element and throws when multiple matches are found.

46
Mid-LevelIntermediate

What does Last do in LINQ?

Last returns the final element of a sequence, or the final element satisfying a predicate. It throws if no matching element exists.

47
Mid-LevelIntermediate

What does LastOrDefault do?

LastOrDefault returns the final matching element or the default value if no element exists. Like Last, its meaning depends on the sequence's order.

48
Mid-LevelIntermediate

What does ElementAt do?

ElementAt returns the element at a specified zero-based position. It throws ArgumentOutOfRangeException when the index is outside the sequence.

49
Mid-LevelIntermediate

What does ElementAtOrDefault do?

ElementAtOrDefault returns the element at the specified zero-based position, or the default value when the index is outside the valid range.

50
Mid-LevelIntermediate

How should you choose between First, Single, and their OrDefault versions?

Choose based on the cardinality your business rule expects. First means at least one result is required but additional matches are acceptable. Single means exactly one result is required. FirstOrDefault means zero or more matches are possible and only the first is needed. SingleOrDefault means zero or one match is valid, while duplicates are an error.