Bagikan melalui


Queryable.TakeWhile Metode

Definisi

Mengembalikan elemen dari urutan selama kondisi yang ditentukan benar, lalu melewati elemen yang tersisa.

Overload

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

Mengembalikan elemen dari urutan selama kondisi yang ditentukan adalah true.

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

Mengembalikan elemen dari urutan selama kondisi yang ditentukan adalah true. Indeks elemen digunakan dalam logika fungsi predikat.

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

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Mengembalikan elemen dari urutan selama kondisi yang ditentukan adalah true.

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)

Jenis parameter

TSource

Jenis elemen source.

Parameter

source
IQueryable<TSource>

Urutan untuk mengembalikan elemen dari.

predicate
Expression<Func<TSource,Boolean>>

Fungsi untuk menguji setiap elemen untuk kondisi.

Mengembalikan

IQueryable<TSource>

Yang IQueryable<T> berisi elemen dari urutan input yang terjadi sebelum elemen di mana pengujian yang ditentukan oleh predicate tidak lagi lulus.

Atribut

Pengecualian

source atau predicate adalah null.

Contoh

Contoh kode berikut menunjukkan cara menggunakan TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) untuk mengembalikan elemen dari awal urutan selama kondisinya benar.

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

Keterangan

Metode ini memiliki setidaknya satu parameter jenis Expression<TDelegate> yang argumen jenisnya adalah salah satu jenisnya Func<T,TResult> . Untuk parameter ini, Anda dapat meneruskan ekspresi lambda dan akan dikompilasi ke Expression<TDelegate>.

Metode ini TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) menghasilkan MethodCallExpression yang mewakili pemanggilan TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) dirinya sebagai metode generik yang dibangun. Kemudian meneruskan MethodCallExpression ke CreateQuery(Expression) metode yang diwakili IQueryProvider oleh Provider properti source parameter.

Perilaku kueri yang terjadi sebagai akibat dari menjalankan pohon ekspresi yang mewakili panggilan TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) tergantung pada implementasi jenis source parameter. Perilaku yang diharapkan adalah bahwa itu berlaku predicate untuk setiap elemen di source sampai menemukan elemen yang predicate mengembalikan false. Ini mengembalikan semua elemen hingga titik tersebut.

Berlaku untuk

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

Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs
Sumber:
Queryable.cs

Mengembalikan elemen dari urutan selama kondisi yang ditentukan adalah true. Indeks elemen digunakan dalam logika fungsi predikat.

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)

Jenis parameter

TSource

Jenis elemen source.

Parameter

source
IQueryable<TSource>

Urutan untuk mengembalikan elemen dari.

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

Fungsi untuk menguji setiap elemen untuk kondisi; parameter kedua fungsi mewakili indeks elemen dalam urutan sumber.

Mengembalikan

IQueryable<TSource>

Yang IQueryable<T> berisi elemen dari urutan input yang terjadi sebelum elemen di mana pengujian yang ditentukan oleh predicate tidak lagi lulus.

Atribut

Pengecualian

source atau predicate adalah null.

Contoh

Contoh kode berikut menunjukkan cara menggunakan TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) untuk mengembalikan elemen dari awal urutan selama kondisi yang menggunakan indeks elemen benar.

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

Keterangan

Metode ini memiliki setidaknya satu parameter jenis Expression<TDelegate> yang argumen jenisnya adalah salah satu jenisnya Func<T,TResult> . Untuk parameter ini, Anda dapat meneruskan ekspresi lambda dan akan dikompilasi ke Expression<TDelegate>.

Metode ini TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) menghasilkan MethodCallExpression yang mewakili pemanggilan TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) dirinya sebagai metode generik yang dibangun. Kemudian meneruskan MethodCallExpression ke CreateQuery(Expression) metode yang diwakili IQueryProvider oleh Provider properti source parameter.

Perilaku kueri yang terjadi sebagai akibat dari menjalankan pohon ekspresi yang mewakili panggilan TakeWhile<TSource>(IQueryable<TSource>, Expression<Func<TSource,Int32,Boolean>>) tergantung pada implementasi jenis source parameter. Perilaku yang diharapkan adalah bahwa itu berlaku predicate untuk setiap elemen di source sampai menemukan elemen yang predicate mengembalikan false. Ini mengembalikan semua elemen hingga titik tersebut. Indeks setiap elemen sumber disediakan sebagai argumen kedua untuk predicate.

Berlaku untuk