In LINQ (Language Integrated Query), the where clause is used to filter data from a collection or sequence based on a specified condition. It allows you to narrow down the results to only those elements that meet a particular criterion. The primary purpose of the where clause is to perform data filtering and selection, helping you retrieve a subset of data that matches specific conditions.
Here's how the where clause works in LINQ:
Filtering Data:
You specify a condition within the where clause that acts as a filter. This condition can involve one or more properties or attributes of the elements in the collection.
The condition is evaluated for each element in the collection.
Only elements for which the condition evaluates to true are included in the result.
Syntax:
The basic syntax for using the where clause in a LINQ query is as follows:
var evenNumbers = from elements in collection
where condition
select element;
element represents an individual item in the collection.
collection is the source collection or sequence of data.
condition is the expression that defines the filter criteria.
Examples:
Filtering a list of integers to find even numbers:
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
Filtering a list of objects to find persons older than 18:
var adults = from person in people
where person.Age > 18
select person;
Chaining:
You can chain multiple where clauses together to create complex filtering conditions. Each
where clause further refines the filter criteria.
var result = from item in collection
where condition1
where condition2
select item;
The where clause is a powerful tool in LINQ, as it enables you to express complex filtering logic in a declarative and readable manner. It is commonly used for querying databases, working with collections, and filtering data in various scenarios where data selection is required.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In LINQ (Language Integrated Query), the where clause is used to filter data from a collection or sequence based on a specified condition. It allows you to narrow down the results to only those elements that meet a particular criterion. The primary purpose of the where clause is to perform data filtering and selection, helping you retrieve a subset of data that matches specific conditions.
Here's how the where clause works in LINQ:
Filtering Data:
Syntax:
Examples:
Filtering a list of integers to find even numbers:
Filtering a list of objects to find persons older than 18:
Chaining:
The where clause is a powerful tool in LINQ, as it enables you to express complex filtering logic in a declarative and readable manner. It is commonly used for querying databases, working with collections, and filtering data in various scenarios where data selection is required.