C# Interview Question #44

What is the difference between Where() and Select() in LINQ?

LINQ & Querying Mid-Level Intermediate

Quick Interview Answer

Where filters elements based on a condition. Select transforms or projects each element into another shape or value.

Detailed Explanation

Where filters elements based on a condition. Select transforms or projects each element into another shape or value.

Where normally reduces which records are returned. Select controls what data is returned.

In database queries, using Select to project only required columns can reduce data transfer and improve performance.

Code Example

var expensive = products
    .Where(p => p.Price > 1000);

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

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