Skip to Content
Course content

187: Where and Filtering Predicates

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When you first start working with collections in C#, your instinct is usually to reach for the foreach loop. It's reliable, and you have total control. But as soon as you need to extract a subset of data—say, finding all the "Overdue" invoices in a billing system—that control starts to feel like a chore. You end up writing the same boilerplate over and over: initialize a new empty list, loop through the source, check a condition, and manually add the matching items to the new list.

The manual loop grind

I've seen this pattern in countless code reviews. It usually looks something like this:

var expensiveProducts = new List<Product>();
foreach (var p in allProducts)
{
    if (p.Price > 1000 && p.Category == "Electronics")
    {
        expensiveProducts.Add(p);
    }
}

Now, there is nothing technically "wrong" with this. It works, and it's easy to debug. However, you're spending three or four lines of code describing how to filter the list rather than what you actually want. You're managing the state of a temporary list, handling the iteration, and nesting your logic inside an if block. It's noisy. When you have a file with ten of these blocks, the actual business logic—the price and category check—gets buried in the plumbing.

Declarative filtering with Where

This is where .Where() comes in. It allows you to move from an imperative style (do this, then do that) to a declarative style (give me the items that match this criteria). Instead of building a new list manually, you pass a predicate to the Where method. A predicate is just a fancy name for a function that takes an object and returns a boolean: true if it stays, false if it goes.

Here is how I'd write that same logic today:

var expensiveProducts = allProducts.Where(p => p.Price > 1000 && p.Category == "Electronics");

Notice how the noise is gone. The p => ... part is a lambda expression. I'm telling C#, "For every element p in the collection, apply this logic." It's cleaner, it's more readable, and it's significantly harder to accidentally introduce a bug (like forgetting to initialize the results list) because the framework handles the heavy lifting.

Execution timing and the predicate cost

There is one detail here that often trips people up: Where doesn't actually filter the list the moment you call it. It uses deferred execution. When you write that line, C# isn't looping through your products yet; it's just building a plan for how to filter them later. The actual work happens when you iterate over expensiveProducts in a foreach loop or call .ToList().

You might wonder if this is slower than a manual loop. In 99% of application code, the difference is negligible. You're trading a few CPU cycles for a massive gain in maintainability. The only time I'd suggest going back to the manual loop is if you are in a high-frequency trading loop or a game engine update tick where every nanosecond is audited. For everything else, the clarity of the predicate wins.

  • Imperative: Focuses on the process (create list $\rightarrow$ loop $\rightarrow$ check $\rightarrow$ add).
  • Declarative: Focuses on the result (filter by these criteria).



📋 Practical Task

Filter High-Risk Security Alerts

You are working on a security dashboard. You have a list of SecurityAlert objects, and you need to isolate only the alerts that are marked as "Critical" and occurred within the last 24 hours.

Requirements:

  • Create a class SecurityAlert with properties Severity (string), Timestamp (DateTime), and Message (string).
  • Create a List<SecurityAlert> populated with at least five different alerts (some critical, some low, some old, some recent).
  • Use the .Where() method to filter the list.
  • The predicate should check if Severity == "Critical" AND if the Timestamp is greater than DateTime.Now.AddDays(-1).
  • Print the messages of the filtered alerts to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.