Enumerable.TakeWhile Metodo
Definizione
Restituisce gli elementi da una sequenza finché la condizione specificata è soddisfatta, poi ignora gli elementi rimanenti.Returns elements from a sequence as long as a specified condition is true, and then skips the remaining elements.
Overload
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
Restituisce elementi di una sequenza finché una condizione specificata è soddisfatta.Returns elements from a sequence as long as a specified condition is true. |
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) |
Restituisce elementi di una sequenza finché una condizione specificata è soddisfatta.Returns elements from a sequence as long as a specified condition is true. L'indice dell'elemento viene usato nella logica della funzione predicativa.The element's index is used in the logic of the predicate function. |
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Restituisce elementi di una sequenza finché una condizione specificata è soddisfatta.Returns 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)
Parametri di tipo
- TSource
Tipo degli elementi di source
.The type of the elements of source
.
Parametri
- source
- IEnumerable<TSource>
Sequenza dalla quale vengono restituiti gli elementi.A sequence to return elements from.
Funzione per testare ogni elemento rispetto a una condizione.A function to test each element for a condition.
Restituisce
- IEnumerable<TSource>
Oggetto IEnumerable<T> che contiene gli elementi dalla sequenza di input che precedono il primo elemento che non soddisfa il test.An IEnumerable<T> that contains the elements from the input sequence that occur before the element at which the test no longer passes.
Eccezioni
source
o predicate
è null
.source
or predicate
is null
.
Esempio
Nell'esempio di codice riportato di seguito viene illustrato come utilizzare TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) per restituire elementi dall'inizio di una sequenza purché una condizione sia true.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
Commenti
Questo metodo viene implementato tramite l'esecuzione posticipata.This method is implemented by using deferred execution. Il valore restituito immediato è un oggetto che archivia tutte le informazioni necessarie per eseguire l'azione.The immediate return value is an object that stores all the information that is required to perform the action. La query rappresentata da questo metodo non viene eseguita finché l'oggetto non viene enumerato chiamando il relativo GetEnumerator
metodo direttamente o utilizzando foreach
in Visual C# o For Each
in Visual Basic.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.
Il TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) metodo verifica ogni elemento di utilizzando source
predicate
e restituisce l'elemento se il risultato è 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
. L'enumerazione viene arrestata quando la funzione di predicato restituisce false
per un elemento o quando source
non contiene altri elementi.Enumeration stops when the predicate function returns false
for an element or when source
contains no more elements.
I TakeWhile SkipWhile metodi e sono complementi funzionali.The TakeWhile and SkipWhile methods are functional complements. Data una sequenza coll
e una funzione pura p
, la concatenazione dei risultati di coll.TakeWhile(p)
e coll.SkipWhile(p)
produce la stessa sequenza di 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 sintassi delle espressioni di query una Take While
clausola viene convertita in una chiamata di TakeWhile .In Visual Basic query expression syntax, a Take While
clause translates to an invocation of TakeWhile.
Vedi anche
Si applica a
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>)
Restituisce elementi di una sequenza finché una condizione specificata è soddisfatta.Returns elements from a sequence as long as a specified condition is true. L'indice dell'elemento viene usato nella logica della funzione predicativa.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)
Parametri di tipo
- TSource
Tipo degli elementi di source
.The type of the elements of source
.
Parametri
- source
- IEnumerable<TSource>
Sequenza dalla quale vengono restituiti gli elementi.The sequence to return elements from.
Funzione per verificare ogni elemento di origine per una condizione; il secondo parametro della funzione rappresenta l'indice dell'elemento di origine.A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
Restituisce
- IEnumerable<TSource>
Oggetto IEnumerable<T> che contiene elementi dalla sequenza di input che precedono il primo elemento che non soddisfa il test.An IEnumerable<T> that contains elements from the input sequence that occur before the element at which the test no longer passes.
Eccezioni
source
o predicate
è null
.source
or predicate
is null
.
Esempio
Nell'esempio di codice riportato di seguito viene illustrato come utilizzare TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) per restituire elementi dall'inizio di una sequenza purché una condizione che utilizza l'indice dell'elemento sia true.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
Commenti
Questo metodo viene implementato tramite l'esecuzione posticipata.This method is implemented by using deferred execution. Il valore restituito immediato è un oggetto che archivia tutte le informazioni necessarie per eseguire l'azione.The immediate return value is an object that stores all the information that is required to perform the action. La query rappresentata da questo metodo non viene eseguita finché l'oggetto non viene enumerato chiamando il relativo GetEnumerator
metodo direttamente o utilizzando foreach
in Visual C# o For Each
in Visual Basic.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.
Il TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) metodo verifica ogni elemento di utilizzando source
predicate
e restituisce l'elemento se il risultato è 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
. L'enumerazione viene arrestata quando la funzione di predicato restituisce false
per un elemento o quando source
non contiene altri elementi.Enumeration stops when the predicate function returns false
for an element or when source
contains no more elements.
Il primo argomento di predicate
rappresenta l'elemento da testare.The first argument of predicate
represents the element to test. Il secondo argomento rappresenta l'indice in base zero dell'elemento all'interno di source
.The second argument represents the zero-based index of the element within source
.
I TakeWhile SkipWhile metodi e sono complementi funzionali.The TakeWhile and SkipWhile methods are functional complements. Data una sequenza coll
e una funzione pura p
, la concatenazione dei risultati di coll.TakeWhile(p)
e coll.SkipWhile(p)
produce la stessa sequenza di 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 sintassi delle espressioni di query una Take While
clausola viene convertita in una chiamata di TakeWhile .In Visual Basic query expression syntax, a Take While
clause translates to an invocation of TakeWhile.