👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Where to Filter Data

Using LINQ Where to Filter Data

LINQ

26 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

This article explains how to use the Where method in LINQ to filter data in .NET.

Note: If you haven't already, read the article on Using LINQ OrderBy to Sort Data in collections.

linq where in dotnet

Why we gonna do?

The Where method in LINQ allows filtering collections based on conditions. The resulting collection can be the same, smaller, or even empty. You can chain conditions using logical operators like &&, ||, and !.

How we gonna do?

Filtering by Single Property

Example of filtering a Product class by a single property.

Code Sample - LINQ Where Single Property

Filtering by Multiple Properties

Example of filtering a Product class by multiple properties using AND and OR operators.

Code Sample - LINQ Where Multiple Properties

Custom Extension Method

Create a custom extension method to filter a collection. This method returns an IEnumerable<T> and can be used on the query instead of the Where method.

Code Sample - LINQ Where Custom Extension Method

Demo Space

Summary

This article covered how to filter data in collections using LINQ. We explored filtering with single and multiple fields using AND and OR conditions, and created a custom extension method. These techniques can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • Where
  • Filter