LINQ Interview Question #28
What is the indexed overload of Where?
Filtering, Projection, Ordering & Element Operators Junior Beginner
Quick Interview Answer
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.
Detailed Explanation
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.
The indexed overload is mainly useful for LINQ to Objects. Query providers such as EF Core may not translate every index-based operation, so provider capabilities should be considered.
Code Example
var values = new[] { "A", "B", "C", "D" };
var result = values
.Where((value, index) => index % 2 == 0);
// A, C