Sdílet prostřednictvím


Queryable.TakeWhile Metoda

Definice

Vrátí prvky ze sekvence, pokud je zadaná podmínka pravdivá, a zbývající prvky pak přeskočí.

Přetížení

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

Vrátí prvky ze sekvence, pokud je zadaná podmínka pravdivá.

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

Vrátí prvky ze sekvence, pokud je zadaná podmínka pravdivá. Index elementu se používá v logice predikátové funkce.

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí prvky ze sekvence, pokud je zadaná podmínka pravdivá.

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);
[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> 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>
[<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 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 prvků .source

Parametry

source
IQueryable<TSource>

Sekvence, ze které se mají vrátit prvky.

predicate
Expression<Func<TSource,Boolean>>

Funkce k otestování jednotlivých prvků pro podmínku.

Návraty

IQueryable<TSource>

Obsahuje IQueryable<T> prvky ze vstupní sekvence, ke kterým dochází před prvkem, ve kterém test určený predicate již neprojde.

Atributy

Výjimky

source nebo predicate je null.

Příklady

Následující příklad kódu ukazuje, jak použít TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) k vrácení prvků ze začátku sekvence, pokud je podmínka pravdivá.

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

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda a bude zkompilován do Expression<TDelegate>.

Metoda TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) vygeneruje MethodCallExpression , která představuje samotné volání TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderCreateQuery(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) , závisí na implementaci typu parametru source . Očekávané chování je, že se vztahuje predicate na každý prvek, source dokud nenajde prvek, pro který predicate vrátí false. Vrátí všechny prvky až do tohoto bodu.

Platí pro

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

Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs
Zdroj:
Queryable.cs

Vrátí prvky ze sekvence, pokud je zadaná podmínka pravdivá. Index elementu se používá v logice predikátové funkce.

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);
[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> 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>
[<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 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 prvků .source

Parametry

source
IQueryable<TSource>

Sekvence, ze které se mají vrátit prvky.

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

Funkce k otestování jednotlivých prvků pro podmínku; druhý parametr funkce představuje index prvku ve zdrojové sekvenci.

Návraty

IQueryable<TSource>

Obsahuje IQueryable<T> prvky ze vstupní sekvence, ke kterým dochází před prvkem, ve kterém test určený predicate již neprojde.

Atributy

Výjimky

source nebo predicate je null.

Příklady

Následující příklad kódu ukazuje, jak použít TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) k vrácení prvků od začátku sekvence, pokud podmínka, která používá index prvku je 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

Poznámky

Tato metoda má alespoň jeden parametr typu Expression<TDelegate> , jehož argument typu je jedním z Func<T,TResult> typů. Pro tyto parametry můžete předat výraz lambda a bude zkompilován do Expression<TDelegate>.

Metoda TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) vygeneruje MethodCallExpression , která představuje samotné volání TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) jako vytvořenou obecnou metodu. Pak předá MethodCallExpression metodu IQueryProviderCreateQuery(Expression) reprezentované Provider vlastností parametrusource.

Chování dotazu, ke kterému dochází v důsledku spuštění stromu výrazu, který představuje volání TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) , závisí na implementaci typu parametru source . Očekávané chování je, že se vztahuje predicate na každý prvek, source dokud nenajde prvek, pro který predicate vrátí false. Vrátí všechny prvky až do tohoto bodu. Index každého zdrojového prvku je poskytován jako druhý argument pro predicate.

Platí pro