Enumerable.Where Metoda

Definicja

Filtruje sekwencję wartości na podstawie predykatu.

Przeciążenia

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

Filtruje sekwencję wartości na podstawie predykatu.

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

Filtruje sekwencję wartości na podstawie predykatu. Indeks każdego elementu jest używany w logice funkcji predykatu.

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

Źródło:
Where.cs
Źródło:
Where.cs
Źródło:
Where.cs

Filtruje sekwencję wartości na podstawie predykatu.

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)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element do filtrowania IEnumerable<T> .

predicate
Func<TSource,Boolean>

Funkcja testowania każdego elementu na stanie.

Zwraca

IEnumerable<TSource>

Element IEnumerable<T> zawierający elementy z sekwencji danych wejściowych, które spełniają warunek.

Wyjątki

source lub predicate ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) metody filtrowania sekwencji.

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

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczone przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach języka C# lub For Each w Visual Basic.

W składni wyrażeń zapytania klauzula where (C#) lub Where (Visual Basic) przekłada się na wywołanie Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)elementu .

Zobacz też

Dotyczy

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

Źródło:
Where.cs
Źródło:
Where.cs
Źródło:
Where.cs

Filtruje sekwencję wartości na podstawie predykatu. Indeks każdego elementu jest używany w logice funkcji predykatu.

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)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IEnumerable<TSource>

Element do filtrowania IEnumerable<T> .

predicate
Func<TSource,Int32,Boolean>

Funkcja do testowania każdego elementu źródłowego dla warunku; drugi parametr funkcji reprezentuje indeks elementu źródłowego.

Zwraca

IEnumerable<TSource>

Element IEnumerable<T> zawierający elementy z sekwencji danych wejściowych, które spełniają warunek.

Wyjątki

source lub predicate ma wartość null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać Where<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) do filtrowania sekwencji na podstawie predykatu, który obejmuje indeks każdego elementu.

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

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczone przez wywołanie metody GetEnumerator bezpośrednio lub przy użyciu foreach języka C# lub For Each w Visual Basic.

Pierwszy argument funkcji predicate reprezentuje element do przetestowania. Drugi argument reprezentuje indeks zerowy elementu w ramach elementu source.

Dotyczy