What is filtering in LINQ?
Filtering selects only the elements that satisfy a condition. The primary LINQ filtering operator is Where.
LINQ interview questions covering filtering, Select, SelectMany, ordering, sorting and element operators such as First, Single, Last and ElementAt.
Open a question for the full answer, code and interview guidance.
Filtering selects only the elements that satisfy a condition. The primary LINQ filtering operator is Where.
Yes. A single Where predicate can combine conditions with Boolean operators such as && and ||. Multiple Where calls can also be chained.
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.
Projection transforms each source element into a desired output shape. Select applies a selector to every element and returns the projected 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>.
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.
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.
SelectMany has overloads that support a result selector, or the projection can create an object containing values from both the parent and child.
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.
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.
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.
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.
Start with OrderBy or OrderByDescending for the primary key and add ThenBy or ThenByDescending for subsequent keys.
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.
First returns the first element of a sequence. An overload can return the first element satisfying a predicate.
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.
First throws an exception when no element exists, while FirstOrDefault returns the default value. The choice should reflect the semantics of the application.
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.
SingleOrDefault returns the only matching element or the default value when no element exists, but it still throws if more than one element matches.
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.
Last returns the final element of a sequence, or the final element satisfying a predicate. It throws if no matching element exists.
LastOrDefault returns the final matching element or the default value if no element exists. Like Last, its meaning depends on the sequence's order.
ElementAt returns the element at a specified zero-based position. It throws ArgumentOutOfRangeException when the index is outside the sequence.
ElementAtOrDefault returns the element at the specified zero-based position, or the default value when the index is outside the valid range.
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.