LINQ Interview Question #29

What is projection with Select?

Filtering, Projection, Ordering & Element Operators Junior Beginner

Quick Interview Answer

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

Detailed Explanation

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

The result can contain scalar values, DTOs, anonymous types, tuples, or another named type. In database queries, projection is also important for retrieving only required columns.

Code Example

var result = products.Select(p => new ProductDto
{
    Id = p.Id,
    Name = p.Name,
    Price = p.Price
});