Enumerable.TakeWhile Methode
Definition
Gibt Elemente aus einer Sequenz zurück, solange eine angegebene Bedingung TRUE ist, und überspringt dann die übrigen ElementeReturns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.
Überlädt
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
Gibt Elemente aus einer Sequenz zurück, solange eine angegebene Bedingung TRUE istReturns elements from a sequence as long as a specified condition is true. |
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) |
Gibt Elemente aus einer Sequenz zurück, solange eine angegebene Bedingung TRUE istReturns elements from a sequence as long as a specified condition is true. In der Logik der Prädikatfunktion wird der Index des Elements verwendet.The element's index is used in the logic of the predicate function. |
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Gibt Elemente aus einer Sequenz zurück, solange eine angegebene Bedingung TRUE istReturns elements from a sequence as long as a specified condition is true.
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)
Typparameter
- TSource
Der Typ der Elemente von source
.The type of the elements of source
.
Parameter
- source
- IEnumerable<TSource>
Eine Sequenz, aus der Elemente zurückgegeben werden sollenA sequence to return elements from.
Eine Funktion, mit der jedes Element auf eine Bedingung überprüft wird.A function to test each element for a condition.
Gibt zurück
- IEnumerable<TSource>
Ein IEnumerable<T>, das die Elemente aus der Eingabesequenz enthält, die vor dem Element auftreten, bei dem die Überprüfung nicht mehr erfolgreich istAn IEnumerable<T> that contains the elements from the input sequence that occur before the element at which the test no longer passes.
Ausnahmen
source
oder predicate
ist null
.source
or predicate
is null
.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie verwendet TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) wird, um Elemente ab dem Anfang einer Sequenz zurückzugeben, solange eine Bedingung true ist.The following code example demonstrates how to use TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) to return elements from the start of a sequence as long as a condition is true.
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
Hinweise
Diese Methode wird mithilfe von verzögerter Ausführung implementiert.This method is implemented by using deferred execution. Der unmittelbare Rückgabewert ist ein Objekt, das alle Informationen speichert, die zum Ausführen der Aktion erforderlich sind.The immediate return value is an object that stores all the information that is required to perform the action. Die durch diese Methode dargestellte Abfrage wird erst ausgeführt, wenn das Objekt durch Aufrufen der- GetEnumerator
Methode direkt oder mithilfe von foreach
in Visual c# oder in Visual Basic aufgezählt wird For Each
.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator
method directly or by using foreach
in Visual C# or For Each
in Visual Basic.
Die TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) -Methode testet jedes Element von source
mithilfe von predicate
und gibt das-Element zurück, wenn das Ergebnis ist true
.The TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method tests each element of source
by using predicate
and yields the element if the result is true
. Die Enumeration wird beendet, wenn die Prädikat Funktion false
für ein Element zurückgibt oder wenn source
keine weiteren Elemente enthält.Enumeration stops when the predicate function returns false
for an element or when source
contains no more elements.
Die TakeWhile -Methode und die- SkipWhile Methode sind funktionale Ergänzungen.The TakeWhile and SkipWhile methods are functional complements. Bei einer Sequenz coll
und einer reinen Funktion p
ergibt die Verkettung der Ergebnisse von coll.TakeWhile(p)
und coll.SkipWhile(p)
die gleiche Sequenz wie coll
.Given a sequence coll
and a pure function p
, concatenating the results of coll.TakeWhile(p)
and coll.SkipWhile(p)
yields the same sequence as coll
.
In Visual Basic Abfrage Ausdruckssyntax übersetzt eine- Take While
Klausel in einen Aufruf von TakeWhile .In Visual Basic query expression syntax, a Take While
clause translates to an invocation of TakeWhile.
Siehe auch
Gilt für:
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Gibt Elemente aus einer Sequenz zurück, solange eine angegebene Bedingung TRUE istReturns elements from a sequence as long as a specified condition is true. In der Logik der Prädikatfunktion wird der Index des Elements verwendet.The element's index is used in the logic of the predicate function.
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)
Typparameter
- TSource
Der Typ der Elemente von source
.The type of the elements of source
.
Parameter
- source
- IEnumerable<TSource>
Die Sequenz, aus der Elemente zurückgegeben werden sollen.The sequence to return elements from.
Eine Funktion zum Überprüfen jedes Quellelements auf eine Bedingung. Der zweite Parameter der Funktion stellt den Index des Quellelements dar.A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Gibt zurück
- IEnumerable<TSource>
Ein IEnumerable<T>, das Elemente aus der Eingabesequenz enthält, die vor dem Element auftreten, bei dem die Überprüfung nicht mehr erfolgreich istAn IEnumerable<T> that contains elements from the input sequence that occur before the element at which the test no longer passes.
Ausnahmen
source
oder predicate
ist null
.source
or predicate
is null
.
Beispiele
Im folgenden Codebeispiel wird veranschaulicht, wie verwendet TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) wird, um Elemente vom Anfang einer Sequenz zurückzugeben, solange eine Bedingung, die den Index des Elements verwendet, true ist.The following code example demonstrates how to use TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) to return elements from the start of a sequence as long as a condition that uses the element's index is true.
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
Hinweise
Diese Methode wird mithilfe von verzögerter Ausführung implementiert.This method is implemented by using deferred execution. Der unmittelbare Rückgabewert ist ein Objekt, das alle Informationen speichert, die zum Ausführen der Aktion erforderlich sind.The immediate return value is an object that stores all the information that is required to perform the action. Die durch diese Methode dargestellte Abfrage wird erst ausgeführt, wenn das Objekt durch Aufrufen der- GetEnumerator
Methode direkt oder mithilfe von foreach
in Visual c# oder in Visual Basic aufgezählt wird For Each
.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator
method directly or by using foreach
in Visual C# or For Each
in Visual Basic.
Die TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) -Methode testet jedes Element von source
mithilfe von predicate
und gibt das-Element zurück, wenn das Ergebnis ist true
.The TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) method tests each element of source
by using predicate
and yields the element if the result is true
. Die Enumeration wird beendet, wenn die Prädikat Funktion false
für ein Element zurückgibt oder wenn source
keine weiteren Elemente enthält.Enumeration stops when the predicate function returns false
for an element or when source
contains no more elements.
Das erste Argument von predicate
stellt das zu testende Element dar.The first argument of predicate
represents the element to test. Das zweite Argument stellt den NULL basierten Index des Elements innerhalb von dar source
.The second argument represents the zero-based index of the element within source
.
Die TakeWhile -Methode und die- SkipWhile Methode sind funktionale Ergänzungen.The TakeWhile and SkipWhile methods are functional complements. Bei einer Sequenz coll
und einer reinen Funktion p
ergibt die Verkettung der Ergebnisse von coll.TakeWhile(p)
und coll.SkipWhile(p)
die gleiche Sequenz wie coll
.Given a sequence coll
and a pure function p
, concatenating the results of coll.TakeWhile(p)
and coll.SkipWhile(p)
yields the same sequence as coll
.
In Visual Basic Abfrage Ausdruckssyntax übersetzt eine- Take While
Klausel in einen Aufruf von TakeWhile .In Visual Basic query expression syntax, a Take While
clause translates to an invocation of TakeWhile.