Where and Filtering
Select matching elements.
Where and Filtering is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Where Does
Where is the core LINQ filtering operator. It takes a predicate — a function returning bool — and yields only the elements that satisfy it.
It lives in System.Linq and works on any IEnumerable<T>, returning a new sequence rather than mutating the source.
using System;
using System.Linq;
using System.Collections.Generic;
var nums = new List<int> { 1, 2, 3, 4, 5, 6 };
var evens = nums.Where(n => n % 2 == 0);
Console.WriteLine(string.Join(", ", evens));The Predicate Lambda
The argument to Where is a lambda n => condition. The parameter represents each element, and the body must evaluate to true or false.
Returning true keeps the element; false drops it. Any boolean expression works.
using System;
using System.Linq;
string[] words = { "apple", "fig", "banana", "kiwi" };
var shortWords = words.Where(w => w.Length <= 4);
Console.WriteLine(string.Join(", ", shortWords));Query Syntax Form
LINQ offers a query syntax that reads like SQL. A where clause filters, and select projects.
It compiles to the same method calls, so from x in src where ... select x is identical to src.Where(...). Use whichever reads clearer.
using System;
using System.Linq;
int[] scores = { 40, 75, 90, 55, 88 };
var passing = from s in scores
where s >= 60
select s;
Console.WriteLine(string.Join(", ", passing));Combining Conditions
Inside a single predicate you can combine multiple checks with && and ||. This keeps related filtering in one pass.
Parentheses help readability when mixing operators, just as in any boolean expression.
using System;
using System.Linq;
using System.Collections.Generic;
var ages = new List<int> { 12, 17, 21, 35, 64, 70 };
var adults = ages.Where(a => a >= 18 && a < 65);
Console.WriteLine(string.Join(", ", adults));Chaining Where Calls
You can chain multiple Where calls. Each filters the result of the previous one, so the conditions combine with logical AND.
Chaining is sometimes clearer than one big predicate, and lets you compose filters built in different places.
using System;
using System.Linq;
int[] data = { 3, 8, 15, 22, 27, 40 };
var result = data.Where(n => n > 5)
.Where(n => n % 2 == 0);
Console.WriteLine(string.Join(", ", result));Where with Index
An overload of Where exposes the element's zero-based index as a second lambda parameter. This is handy for position-based filtering.
Use it to keep, say, every other element or to skip the first item by index.
using System;
using System.Linq;
string[] items = { "a", "b", "c", "d", "e" };
var evenIdx = items.Where((val, idx) => idx % 2 == 0);
Console.WriteLine(string.Join(", ", evenIdx));Filtering Objects
Filtering shines on collections of objects. The predicate can read any property of each element.
This is how you express business rules — "customers in this city", "orders above a threshold" — declaratively.
using System;
using System.Linq;
using System.Collections.Generic;
record Product(string Name, decimal Price);
var list = new List<Product> {
new("Pen", 1.5m), new("Lamp", 20m), new("Mug", 8m) };
var cheap = list.Where(p => p.Price < 10);
foreach (var p in cheap) Console.WriteLine(p.Name);Deferred Execution
Where uses deferred execution: it does not run when defined, only when the sequence is iterated (for example in a foreach or by calling ToList).
This means changes to the source before iteration are reflected in the result.
using System;
using System.Linq;
using System.Collections.Generic;
var src = new List<int> { 1, 2 };
var q = src.Where(n => n > 1);
src.Add(5);
Console.WriteLine(string.Join(", ", q));Re-evaluation Cost
Because filtering is deferred, iterating the same query twice re-runs the predicate twice. For expensive predicates or live sources this can surprise you.
When you need a stable, reusable snapshot, materialize once with ToList — covered later in this course.
using System;
using System.Linq;
int calls = 0;
int[] nums = { 1, 2, 3 };
var q = nums.Where(n => { calls++; return n > 1; });
foreach (var _ in q) { }
foreach (var _ in q) { }
Console.WriteLine(calls);Filter Then Count
A common pattern is filtering and then asking how many matched. Count can take a predicate directly, fusing filter and count.
count.Count(p => cond) is equivalent to seq.Where(p => cond).Count() but reads tighter.
using System;
using System.Linq;
int[] grades = { 55, 80, 42, 91, 67 };
int passed = grades.Count(g => g >= 60);
Console.WriteLine($"Passed: {passed}");Empty Results Are Fine
If no element satisfies the predicate, Where simply yields an empty sequence — never null.
That makes filters safe to chain: an empty result flows through later operators without special handling.
using System;
using System.Linq;
int[] nums = { 1, 2, 3 };
var big = nums.Where(n => n > 100);
Console.WriteLine($"Count: {big.Count()}");Quick Check
Test your understanding of deferred execution.
Recap
Where filters a sequence by a boolean predicate, returning a new IEnumerable. It has an index overload, can be chained or combined with &&/||, and works on objects via their properties.
Crucially it is deferred — it runs at iteration, so re-iterating re-evaluates. Next we shape what we keep with Select.
Frequently asked questions
Is the “Where and Filtering” lesson free?
Yes — the full text of “Where and Filtering” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Where and Filtering”?
Select matching elements. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Where and Filtering” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Where and Filtering
- Select and Projection
- OrderBy and Grouping
- Aggregates and ToList