Enumerable.TakeWhile 메서드

정의

지정된 조건이 true인 동안 시퀀스의 요소를 반환하고 나머지 요소를 건너뜁니다.

오버로드

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

지정된 조건이 true인 동안 시퀀스에서 요소를 반환합니다. 조건자 함수의 논리에 요소의 인덱스가 사용됩니다.

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

지정된 조건이 true인 동안 시퀀스에서 요소를 반환합니다.

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

지정된 조건이 true인 동안 시퀀스에서 요소를 반환합니다. 조건자 함수의 논리에 요소의 인덱스가 사용됩니다.

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)

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

요소가 반환되는 시퀀스입니다.

predicate
Func<TSource,Int32,Boolean>

각 소스 요소를 조건에 대해 테스트할 함수이며, 이 함수의 두 번째 매개 변수는 소스 요소의 인덱스를 나타냅니다.

반환

IEnumerable<TSource>

입력 시퀀스에서 요소가 테스트를 더 이상 통과하지 않는 위치보다 앞에 나오는 요소가 들어 있는 IEnumerable<T>입니다.

예외

source 또는 predicatenull인 경우

예제

다음 코드 예제에서는 사용 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) 하는 방법에 설명 합니다 요소의 인덱스를 사용 하는 조건이 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

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

메서드는 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Int32,Boolean>) 를 사용하여 predicatesource 각 요소를 테스트하고 결과가 인 경우 요소를 생성합니다true. 조건자 함수가 요소에 대해 반환 false 되거나 요소가 더 이상 포함되지 않은 경우 source 열거형이 중지됩니다.

predicate 첫 번째 인수는 테스트할 요소를 나타냅니다. 두 번째 인수는 내 source요소의 인덱스(0부터 시작)를 나타냅니다.

SkipWhile 메서드는 TakeWhile 기능 보완입니다. 컬렉션 시퀀스와 coll 순수 함수 p가 지정된 경우 및 의 coll.TakeWhile(p) 결과를 연결하면 와 coll.SkipWhile(p) 동일한 시퀀스가 coll생성됩니다.

Visual Basic 쿼리 식 구문에서 절은 Take While 의 호출 TakeWhile로 변환됩니다.

추가 정보

적용 대상

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

지정된 조건이 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)

형식 매개 변수

TSource

source 요소의 형식입니다.

매개 변수

source
IEnumerable<TSource>

요소가 반환되는 시퀀스입니다.

predicate
Func<TSource,Boolean>

각 요소를 조건에 대해 테스트하는 함수입니다.

반환

IEnumerable<TSource>

입력 시퀀스에서 요소가 테스트를 더 이상 통과하지 않는 위치보다 앞에 나오는 요소가 들어 있는 IEnumerable<T>입니다.

예외

source 또는 predicatenull인 경우

예제

다음 코드 예제에서는 조건이 true인 경우 를 사용하여 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 시퀀스 시작부터 요소를 반환하는 방법을 보여 줍니다.

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

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다. 이 메서드가 나타내는 쿼리는 개체를 직접 호출 GetEnumerator 하거나 C# 또는 For Each Visual Basic에서 를 사용하여 foreach 개체를 열거할 때까지 실행되지 않습니다.

메서드는 TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 를 사용하여 predicatesource 각 요소를 테스트하고 결과가 인 경우 요소를 생성합니다true. 조건자 함수가 요소에 대해 반환 false 되거나 요소가 더 이상 포함되지 않은 경우 source 열거형이 중지됩니다.

SkipWhile 메서드는 TakeWhile 기능 보완입니다. 컬렉션 시퀀스와 coll 순수 함수 p가 지정된 경우 및 의 coll.TakeWhile(p) 결과를 연결하면 와 coll.SkipWhile(p) 동일한 시퀀스가 coll생성됩니다.

Visual Basic 쿼리 식 구문에서 절은 Take While 의 호출 TakeWhile로 변환됩니다.

추가 정보

적용 대상