Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Where and Select

โญ beginnerโณ 13 min read๐Ÿ“ Lesson 58 of 85

Two methods cover most data work: keep some elements, transform the rest.

Filter with Where

var nums = new List<int> { 1, 2, 3, 4, 5, 6 };
var evens = nums.Where(n => n % 2 == 0);   // 2, 4, 6

Where takes a predicate โ€” a function from element to bool โ€” and yields elements where it returns true. The source is unchanged; the result is a query over it.

Project with Select

var names = new[] { "an", "binh" };
var upper = names.Select(s => s.ToUpperInvariant());   // "AN", "BINH"
var lengths = names.Select(s => s.Length);             // 2, 4

Select transforms each element into something else โ€” same count in, same count out, new shape out. Together they compose:

var longUpper = names.Where(s => s.Length > 2).Select(s => s.ToUpperInvariant());

Read the chain left to right: filter first, then transform. Chaining is the everyday LINQ style; each stage's output is the next stage's input.

What LINQ actually is

No magic: Where/Select are ordinary extension methods on IEnumerable<T> from System.Linq. The lambda you pass is a delegate that runs per element. Deferred execution is the one surprise โ€” covered two lessons ahead.