LINQ Interview Question #20

What does the Select operator do?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

Select projects every source element into a new value or shape. The output type can be the same as the input type or completely different.

Detailed Explanation

Select projects every source element into a new value or shape. The output type can be the same as the input type or completely different.

Select is commonly used to create DTOs, anonymous types, scalar values, or calculated results. With EF Core, projecting only required columns can reduce transferred data and tracking overhead.

Code Example

var names = products
    .Select(p => p.Name);

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