Queryable.TakeWhile Metoda

Definicja

Zwraca elementy z sekwencji, o ile określony warunek jest spełniony, a następnie pomija pozostałe elementy.

Przeciążenia

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

Zwraca elementy z sekwencji, o ile określony warunek jest spełniony.

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

Zwraca elementy z sekwencji, o ile określony warunek jest spełniony. Indeks elementu jest używany w logice funkcji predykatu.

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

Zwraca elementy z sekwencji, o ile określony warunek jest spełniony.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ TakeWhile(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function TakeWhile(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>

Sekwencja zwracania elementów z.

predicate
Expression<Func<TSource,Boolean>>

Funkcja testowania każdego elementu na stanie.

Zwraca

IQueryable<TSource>

Element IQueryable<T> , który zawiera elementy z sekwencji wejściowej występującej przed elementem, w którym test określony przez predicate nie przechodzi już.

Wyjątki

source lub predicate to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) funkcji zwracania elementów od początku sekwencji, o ile warunek jest spełniony.

string[] fruits = { "apple", "banana", "mango", "orange",
                      "passionfruit", "grape" };

// Take strings from the array until a string
// that is equal to "orange" is found.
IEnumerable<string> query =
    fruits.AsQueryable()
    .TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);

foreach (string fruit in query)
    Console.WriteLine(fruit);

/*
    This code produces the following output:

    apple
    banana
    mango
*/
Dim fruits() As String = {"apple", "banana", "mango", "orange", _
                      "passionfruit", "grape"}

' Take strings from the array until a string
' that is equal to "orange" is found.
Dim query = fruits.AsQueryable() _
    .TakeWhile(Function(fruit) String.Compare("orange", fruit, True) <> 0)

Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next

' Display the output.
MsgBox(output.ToString())

'This code produces the following output:

'apple
'banana
'mango

Uwagi

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

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

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że dotyczy predicate każdego elementu, source dopóki nie znajdzie elementu, dla którego predicate zwraca falsewartość . Zwraca wszystkie elementy do tego momentu.

Dotyczy

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

Zwraca elementy z sekwencji, o ile określony warunek jest spełniony. Indeks elementu jest używany w logice funkcji predykatu.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ TakeWhile(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, int, bool> ^> ^ predicate);
public static System.Linq.IQueryable<TSource> TakeWhile<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,bool>> predicate);
static member TakeWhile : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, int, bool>> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function TakeWhile(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>

Sekwencja zwracania elementów z.

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> , który zawiera elementy z sekwencji wejściowej występującej przed elementem, w którym test określony przez predicate nie przechodzi już.

Wyjątki

source lub predicate to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) funkcji zwracania elementów od początku sekwencji, o ile warunek używający indeksu elementu ma wartość true.

string[] fruits = { "apple", "passionfruit", "banana", "mango",
                      "orange", "blueberry", "grape", "strawberry" };

// Take strings from the array until a string whose length
// is less than its index in the array is found.
IEnumerable<string> query =
    fruits.AsQueryable()
    .TakeWhile((fruit, index) => fruit.Length >= index);

foreach (string fruit in query)
    Console.WriteLine(fruit);

/*
    This code produces the following output:

    apple
    passionfruit
    banana
    mango
    orange
    blueberry
*/
Dim fruits() As String = _
    {"apple", "passionfruit", "banana", "mango", _
     "orange", "blueberry", "grape", "strawberry"}

' Take strings from the array until a string whose length
' is less than its index in the array is found.
Dim query = fruits.AsQueryable() _
    .TakeWhile(Function(fruit, index) fruit.Length >= index)

' 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
' passionfruit
' banana
' mango
' orange
' blueberry

Uwagi

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

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

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że dotyczy predicate każdego elementu, source dopóki nie znajdzie elementu, dla którego predicate zwraca falsewartość . Zwraca wszystkie elementy do tego momentu. Indeks każdego elementu źródłowego jest udostępniany jako drugi argument do predicate.

Dotyczy