Enumerable.TakeWhile Yöntem

Tanım

Belirtilen koşul true olduğu sürece dizideki öğeleri döndürür ve kalan öğeleri atlar.

Aşırı Yüklemeler

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

Belirtilen koşul true olduğu sürece bir diziden öğe döndürür. Öğenin dizini koşul işlevinin mantığında kullanılır.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Belirtilen koşul true olduğu sürece bir diziden öğe döndürür.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)

Belirtilen koşul true olduğu sürece bir diziden öğe döndürür. Öğenin dizini koşul işlevinin mantığında kullanılır.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ TakeWhile(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,bool> predicate);
static member TakeWhile : seq<'Source> * Func<'Source, int, bool> -> seq<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Integer, Boolean)) As IEnumerable(Of TSource)

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

Öğesinden öğe döndürülecek dizi.

predicate
Func<TSource,Int32,Boolean>

Bir koşul için her kaynak öğeyi test etmek için bir işlev; işlevinin ikinci parametresi, kaynak öğenin dizinini temsil eder.

Döndürülenler

IEnumerable<TSource>

IEnumerable<T> Testin artık geçildiği öğeden önce gelen giriş dizisinden öğeleri içeren bir.

Özel durumlar

source veya predicate şeklindedir null.

Örnekler

Aşağıdaki kod örneği, öğesinin dizinini kullanan bir koşul true olduğu sürece bir dizinin başlangıcından öğeleri döndürmek için nasıl kullanılacağını TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) gösterir.

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

IEnumerable<string> query =
    fruits.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
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "passionfruit", "banana", "mango",
 "orange", "blueberry", "grape", "strawberry"}

' Take strings from the array until one
' of the string's lengths is greater than or
' equal to the string item's index in the array.
Dim query As IEnumerable(Of String) =
fruits.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
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' passionfruit
' banana
' mango
' orange
' blueberry

Açıklamalar

Bu yöntem ertelenmiş yürütme kullanılarak uygulanır. Anında dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, doğrudan yöntemini çağırarak GetEnumerator veya C# içinde veya For Each Visual Basic'te kullanarak foreach nesne numaralandırılana kadar yürütülür.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) yöntemi kullanarak öğesinin sourcepredicate her öğesini test eder ve sonuç ise öğesini verirtrue. Koşul işlevi bir öğe için döndürdüğünde false veya başka öğe içermediğinde source numaralandırma durdurulur.

öğesinin predicate ilk bağımsız değişkeni, test yapılacak öğeyi temsil eder. İkinci bağımsız değişken içindeki sourceöğesinin sıfır tabanlı dizinini temsil eder.

TakeWhile ve SkipWhile yöntemleri işlevsel tamamlayıcılardır. Bir koleksiyon dizisi coll ve saf bir işlev pverilip sonuçlarını coll.TakeWhile(p) birleştirir ve coll.SkipWhile(p) ile collaynı diziyi verir.

Visual Basic sorgu ifadesi söz diziminde yan Take While tümcesi çağrısına çevrilir TakeWhile.

Ayrıca bkz.

Şunlara uygulanır

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

Belirtilen koşul true olduğu sürece bir diziden öğe döndürür.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ TakeWhile(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static System.Collections.Generic.IEnumerable<TSource> TakeWhile<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member TakeWhile : seq<'Source> * Func<'Source, bool> -> seq<'Source>
<Extension()>
Public Function TakeWhile(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As IEnumerable(Of TSource)

Tür Parametreleri

TSource

öğelerinin sourcetürü.

Parametreler

source
IEnumerable<TSource>

Öğesinden öğe döndürülecek bir dizi.

predicate
Func<TSource,Boolean>

Bir koşul için her öğeyi test etmek için bir işlev.

Döndürülenler

IEnumerable<TSource>

Testin IEnumerable<T> artık geçildiği öğeden önce oluşan giriş dizisinden öğeleri içeren bir.

Özel durumlar

source veya predicate şeklindedir null.

Örnekler

Aşağıdaki kod örneği, bir koşul true olduğu sürece bir dizinin başından öğeleri döndürmek için nasıl kullanılacağını TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) gösterir.

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

IEnumerable<string> query =
    fruits.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
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Take strings from the array until one of
' the strings matches "orange".
Dim query As IEnumerable(Of String) =
fruits.TakeWhile(Function(fruit) _
                     String.Compare("orange", fruit, True) <> 0)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' banana
' mango

Açıklamalar

Bu yöntem ertelenmiş yürütme kullanılarak uygulanır. Anında dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, doğrudan yöntemini çağırarak GetEnumerator veya C# içinde veya For Each Visual Basic'te kullanarak foreach nesne numaralandırılana kadar yürütülür.

TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) yöntemi kullanarak öğesinin sourcepredicate her öğesini test eder ve sonuç ise öğesini verirtrue. Koşul işlevi bir öğe için döndürdüğünde false veya başka öğe içermediğinde source numaralandırma durdurulur.

TakeWhile ve SkipWhile yöntemleri işlevsel tamamlayıcılardır. Bir koleksiyon dizisi coll ve saf bir işlev pverilip sonuçlarını coll.TakeWhile(p) birleştirir ve coll.SkipWhile(p) ile collaynı diziyi verir.

Visual Basic sorgu ifadesi söz diziminde yan Take While tümcesi çağrısına çevrilir TakeWhile.

Ayrıca bkz.

Şunlara uygulanır