Filtering Data in C# with LINQ
Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition. It's also referred to as selecting elements that match the specified condition.
Important
These samples use an System.Collections.Generic.IEnumerable<T> data source. Data sources based on System.Linq.IQueryProvider use System.Linq.IQueryable<T> data sources and expression trees. Expression trees have limitations on the allowed C# syntax. Furthermore, each IQueryProvider
data source, such as EF Core may impose more restrictions. Check the documentation for your data source.
The following illustration shows the results of filtering a sequence of characters. The predicate for the filtering operation specifies that the character must be 'A'.
The standard query operator methods that perform selection are listed in the following table:
Method Name | Description | C# Query Expression Syntax | More Information |
---|---|---|---|
OfType | Selects values, depending on their ability to be cast to a specified type. | Not applicable. | Enumerable.OfType Queryable.OfType |
Where | Selects values that are based on a predicate function. | where |
Enumerable.Where Queryable.Where |
The following example uses the where
clause to filter from an array those strings that have a specific length.
string[] words = ["the", "quick", "brown", "fox", "jumps"];
IEnumerable<string> query = from word in words
where word.Length == 3
select word;
foreach (string str in query)
{
Console.WriteLine(str);
}
/* This code produces the following output:
the
fox
*/
The equivalent query using method syntax is shown in the following code:
string[] words = ["the", "quick", "brown", "fox", "jumps"];
IEnumerable<string> query =
words.Where(word => word.Length == 3);
foreach (string str in query)
{
Console.WriteLine(str);
}
/* This code produces the following output:
the
fox
*/