Enumerable.Where メソッド

定義

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

オーバーロード

Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

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

Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

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

Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

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

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

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

戻り値

IEnumerable<TSource>

条件を満たす、入力シーケンスの要素を含む IEnumerable<T>

例外

source または predicatenull です。

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

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

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}
/*
 This code produces the following output:

 apple
 mango
 grape
*/
' Create a list of strings.
Dim fruits As New List(Of String)(New String() _
                    {"apple", "passionfruit", "banana", "mango",
                     "orange", "blueberry", "grape", "strawberry"})

' Restrict the results to those strings whose
' length is less than six.
Dim query As IEnumerable(Of String) =
fruits.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
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' mango
' grape

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

クエリ式の構文では、 where (C#) 句または Where (Visual Basic) 句が の Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)呼び出しに変換されます。

こちらもご覧ください

適用対象

Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

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

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

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

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

predicate
Func<TSource,Int32,Boolean>

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

戻り値

IEnumerable<TSource>

条件を満たす、入力シーケンスの要素を含む IEnumerable<T>

例外

source または predicatenull です。

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

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

IEnumerable<int> query =
    numbers.Where((number, index) => number <= index * 10);

foreach (int number in query)
{
    Console.WriteLine(number);
}
/*
 This code produces the following output:

 0
 20
 15
 40
*/
' Create an array of integers.
Dim numbers() As Integer = {0, 30, 20, 15, 90, 85, 40, 75}

' Restrict the results to those numbers whose
' values are less than or equal to their index times 10.
Dim query As IEnumerable(Of Integer) =
numbers.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
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' 0
' 20
' 15
' 40

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

の最初の predicate 引数は、テストする要素を表します。 2 番目の引数は、 内 sourceの要素の 0 から始まるインデックスを表します。

適用対象