Queryable.Where メソッド

定義

述語に基づいて値のシーケンスをフィルター処理します。

オーバーロード

Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

述語に基づいて値のシーケンスをフィルター処理します。

Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)

述語に基づいて値のシーケンスをフィルター処理します。 各要素のインデックスは、述語関数のロジックで使用されます。

Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

述語に基づいて値のシーケンスをフィルター処理します。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Where(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> Where<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Where : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Where(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As IQueryable(Of TSource)

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

フィルター処理する IQueryable<T>

predicate
Expression<Func<TSource,Boolean>>

各要素が条件を満たしているかどうかをテストする関数。

戻り値

IQueryable<TSource>

predicate で指定された条件を満たす、入力シーケンスの要素を含む IQueryable<T>

例外

source または predicatenull です。

次のコード例では、 を使用 Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) してシーケンスをフィルター処理する方法を示します。

List<string> fruits =
    new List<string> { "apple", "passionfruit", "banana", "mango",
                       "orange", "blueberry", "grape", "strawberry" };

// Get all strings whose length is less than 6.
IEnumerable<string> query =
    fruits.AsQueryable().Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
    Console.WriteLine(fruit);

/*
    This code produces the following output:

    apple
    mango
    grape
*/
Dim fruits As New List(Of String)(New String() _
                        {"apple", "passionfruit", "banana", "mango", _
                         "orange", "blueberry", "grape", "strawberry"})

' Get all strings whose length is less than 6.
Dim query = fruits.AsQueryable().Where(Function(fruit) fruit.Length < 6)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
MsgBox(output.ToString())

' This code produces the following output:

' apple
' mango
' grape

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionCreateQuery(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 でpredicate指定された条件を満たす からsource要素を返すということです。

適用対象

Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)

述語に基づいて値のシーケンスをフィルター処理します。 各要素のインデックスは、述語関数のロジックで使用されます。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Where(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, int, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> Where<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
static member Where : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, int, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Where(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Integer, Boolean))) As IQueryable(Of TSource)

型パラメーター

TSource

source の要素の型。

パラメーター

source
IQueryable<TSource>

フィルター処理する IQueryable<T>

predicate
Expression<Func<TSource,Int32,Boolean>>

各要素が条件を満たしているかどうかをテストする関数。この関数の 2 つ目のパラメーターは、ソース シーケンスの要素のインデックスを表します。

戻り値

IQueryable<TSource>

predicate で指定された条件を満たす、入力シーケンスの要素を含む IQueryable<T>

例外

source または predicatenull です。

次のコード例では、 を使用 Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) して、各要素のインデックスを組み込んだ述語に基づいてシーケンスをフィルター処理する方法を示します。

int[] numbers = { 0, 30, 20, 15, 90, 85, 40, 75 };

// Get all the numbers that are less than or equal to
// the product of their index in the array and 10.
IEnumerable<int> query =
    numbers.AsQueryable()
    .Where((number, index) => number <= index * 10);

foreach (int number in query)
    Console.WriteLine(number);

/*
    This code produces the following output:

    0
    20
    15
    40
*/
Dim numbers() As Integer = {0, 30, 20, 15, 90, 85, 40, 75}

' Get all the numbers that are less than or equal to
' the product of their index in the array and 10.
Dim query = numbers.AsQueryable() _
    .Where(Function(number, index) number <= index * 10)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each number As Integer In query
    output.AppendLine(number)
Next
MsgBox(output.ToString())

' This code produces the following output:

' 0
' 20
' 15
' 40

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionCreateQuery(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 でpredicate指定された条件を満たす からsource要素を返すということです。 各ソース要素のインデックスは、 の 2 番目の引数 predicateとして提供されます。

適用対象