LINQ Interview Question #31

What is the indexed overload of Select?

Filtering, Projection, Ordering & Element Operators Junior Beginner

Quick Interview Answer

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.

Detailed Explanation

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.

As with other index-based overloads, translation support can differ when working with IQueryable providers.

Code Example

var names = new[] { "Ali", "Sara", "John" };

var result = names.Select((name, index) => new
{
    Number = index + 1,
    Name = name
});