Condividi tramite


Queryable.Where Metodo

Definizione

Filtra una sequenza di valori in base a un predicato.

Overload

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

Filtra una sequenza di valori in base a un predicato.

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

Filtra una sequenza di valori in base a un predicato. L'indice di ogni elemento viene usato nella logica della funzione predicato.

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

Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs

Filtra una sequenza di valori in base a un predicato.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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)

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> da filtrare.

predicate
Expression<Func<TSource,Boolean>>

Funzione per testare ogni elemento per una condizione.

Restituisce

IQueryable<TSource>

Oggetto IQueryable<T> che contiene elementi della sequenza di input che soddisfano la condizione specificata da predicate.

Attributi

Eccezioni

source o predicate è null.

Esempio

Nell'esempio di codice seguente viene illustrato come usare Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) per filtrare una sequenza.

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

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.

Il Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al CreateQuery(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che restituisce gli elementi da source che soddisfano la condizione specificata da predicate.

Si applica a

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

Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs

Filtra una sequenza di valori in base a un predicato. L'indice di ogni elemento viene usato nella logica della funzione predicato.

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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
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)

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> da filtrare.

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

Funzione per testare ogni elemento per una condizione; il secondo parametro della funzione rappresenta l'indice dell'elemento nella sequenza di origine.

Restituisce

IQueryable<TSource>

Oggetto IQueryable<T> che contiene elementi della sequenza di input che soddisfano la condizione specificata da predicate.

Attributi

Eccezioni

source o predicate è null.

Esempio

Nell'esempio di codice seguente viene illustrato come usare Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) per filtrare una sequenza in base a un predicato che incorpora l'indice di ogni elemento.

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

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.

Il Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al CreateQuery(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Where<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che restituisce gli elementi da source che soddisfano la condizione specificata da predicate. L'indice di ogni elemento di origine viene fornito come secondo argomento di predicate.

Si applica a