Queryable.Where Metoda

Definicja

Filtruje sekwencję wartości na podstawie predykatu.

Przeciążenia

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

Filtruje sekwencję wartości na podstawie predykatu.

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

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

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

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

Filtruje sekwencję wartości na podstawie predykatu.

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)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element do filtrowania IQueryable<T> .

predicate
Expression<Func<TSource,Boolean>>

Funkcja testowania każdego elementu na stanie.

Zwraca

IQueryable<TSource>

Element IQueryable<T> zawierający elementy z sekwencji wejściowej, które spełniają warunek określony przez predicate.

Wyjątki

source lub predicate ma wartość null.

Przykłady

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

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

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca elementy, source które spełniają warunek określony przez predicate.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.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::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)

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element do filtrowania IQueryable<T> .

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

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

Zwraca

IQueryable<TSource>

Element IQueryable<T> zawierający elementy z sekwencji wejściowej, które spełniają warunek określony przez predicate.

Wyjątki

source lub predicate ma wartość null.

Przykłady

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

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

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. Dla tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) generuje obiekt MethodCallExpression , który reprezentuje wywołanie Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression wartość do CreateQuery(Expression) metody IQueryProvider reprezentowanej przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń reprezentującego wywołanie Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zwraca elementy, source które spełniają warunek określony przez predicate. Indeks każdego elementu źródłowego jest dostarczany jako drugi argument do predicate.

Dotyczy