C#idiomatic C#
filtering-datacsharp/linq/standard-query-operators

LINQ

Filtering Data in C# with LINQ

Filtering, also known as selection, restricts results based on a condition. Learn about the standard query operator methods in LINQ in C# that perform filtering.

  • page csharp/linq/standard-query-operators/filtering-data
  • updated 05/29/2024

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.


author: BillWagner ms.author: wiwagn ms.topic: include ms.date: 05/29/2024

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'.

Diagram that shows a LINQ filtering operation

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. System.Linq.Enumerable.OfType

System.Linq.Queryable.OfType
Where Selects values that are based on a predicate function. where System.Linq.Enumerable.Where

System.Linq.Queryable.Where

The following example uses the where clause to filter from an array those strings that have a specific length.


author: '' ms.author: '' ms.topic: include ms.date: 08/11/2025

sample · csharpdocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/WhereFilter.csFilterExampleQuery

csharp
namespace StandardQueryOperators;
public class WhereFilter
{
    public static void RunAllSnippets()
    {
        WhereSampleQuerySyntax();
        WhereSampleMethodSyntax();
    }

    private static void WhereSampleQuerySyntax()
    { 
        // <FilterExampleQuery>
        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
        */
        // </FilterExampleQuery>
    }

    private static void WhereSampleMethodSyntax()
    {
        // <FilterExampleMethod>
        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
        */
        // </FilterExampleMethod>
    }
}

The equivalent query using method syntax is shown in the following code:

sample · csharpdocs/csharp/linq/standard-query-operators/snippets/standard-query-operators/WhereFilter.csFilterExampleMethod

csharp
namespace StandardQueryOperators;
public class WhereFilter
{
    public static void RunAllSnippets()
    {
        WhereSampleQuerySyntax();
        WhereSampleMethodSyntax();
    }

    private static void WhereSampleQuerySyntax()
    { 
        // <FilterExampleQuery>
        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
        */
        // </FilterExampleQuery>
    }

    private static void WhereSampleMethodSyntax()
    {
        // <FilterExampleMethod>
        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
        */
        // </FilterExampleMethod>
    }
}

See also