C# › LINQ

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

Beginner

Answer

Where() FILTERS a sequence, keeping only elements matching a predicate (returns the SAME element type); Select() PROJECTS/transforms each element into something new (potentially a completely different type) — they're often used together, filtering first, then transforming.
var people = new List<Person> { new("Sam", 30), new("Alex", 15) };
var adultNames = people
    .Where(p => p.Age >= 18)   // filters: keeps only matching Person objects
    .Select(p => p.Name);       // projects: transforms Person -> string
Real-world example Filtering a product catalog to in-stock items, then projecting the result down to just the product names for a dropdown list.

Common follow-ups: Can you use Select() to transform each element into a completely different, more complex object, like an anonymous type?