英語で読む

次の方法で共有


Enumerable.Where メソッド

定義

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

オーバーロード

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

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

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

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

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

ソース:
Where.cs
ソース:
Where.cs
ソース:
Where.cs

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

C#
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

戻り値

IEnumerable<TSource>

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

例外

source または predicatenull です。

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

C#
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
*/

注釈

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

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

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Where.cs
ソース:
Where.cs
ソース:
Where.cs

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

C#
public static System.Collections.Generic.IEnumerable<TSource> Where<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);

型パラメーター

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>) して、各要素のインデックスを含む述語に基づいてシーケンスをフィルター処理する方法を示します。

C#
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
*/

注釈

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

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

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0