Enumerable.Where 方法

定義

根據述詞篩選值序列。

多載

名稱 Description
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
來源:
Where.cs
來源:
Where.cs

根據述詞篩選值序列。

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>

An IEnumerable<T> to filter。

predicate
Func<TSource,Boolean>

一個用來測試每個元素條件的函數。

傳回

IEnumerable<TSource>

一個 IEnumerable<T> 包含輸入序列中滿足條件的元素。

例外狀況

sourcepredicatenull

範例

以下程式碼範例示範如何使用 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# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。

在查詢表達式語法中,where (C#) 或 Where (Visual Basic) 子句可轉譯為 Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 的呼叫。

另請參閱

適用於

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

來源:
Where.cs
來源:
Where.cs
來源:
Where.cs
來源:
Where.cs
來源:
Where.cs

根據述詞篩選值序列。 每個元素的索引都會用於述詞函式的邏輯中。

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>

An IEnumerable<T> to filter。

predicate
Func<TSource,Int32,Boolean>

一個用來測試每個來源元素條件的函數;函數的第二個參數代表該來源元素的索引。

傳回

IEnumerable<TSource>

一個 IEnumerable<T> 包含輸入序列中滿足條件的元素。

例外狀況

sourcepredicatenull

範例

以下程式碼範例示範如何根據 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# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。

predicate 第一個參數代表要測試的元素。 第二個參數代表元素 source的零基索引。

適用於