Filtering Data (C#)
Filtering refers to the operation of restricting the result set to contain only those elements that satisfy a specified condition. It is also known as selection.
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 section.
Methods
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 |
Query Expression Syntax Example
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
*/
See also
- System.Linq
- Standard Query Operators Overview (C#)
- where clause
- Dynamically specify predicate filters at run time
- How to query an assembly's metadata with Reflection (LINQ) (C#)
- How to query for files with a specified attribute or name (C#)
- How to sort or filter text data by any word or field (LINQ) (C#)
.NET feedback
The .NET documentation is open source. Provide feedback here.
Feedback
Submit and view feedback for