다음을 통해 공유


데이터 분할

업데이트: 2007년 11월

LINQ의 분할은 요소를 다시 정렬하지 않고 입력 시퀀스를 두 개의 섹션으로 나눈 다음 섹션 중 하나를 반환하는 작업을 가리킵니다.

다음 그림에서는 문자의 시퀀스에서 세 개의 서로 다른 분할 작업 결과를 보여 줍니다. 첫 번째 작업은 시퀀스에서 처음 세 개의 요소를 반환합니다. 두 번째 작업은 처음 세 개의 요소를 건너뛰고 나머지 요소를 반환합니다. 세 번째 작업은 시퀀스에서 처음 두 개의 요소를 건너뛰고 다음 세 개의 요소를 반환합니다.

LINQ 분할 작업

시퀀스를 분할하는 표준 쿼리 연산자 메서드는 다음 단원에 나열되어 있습니다.

연산자

연산자 이름

설명

C# 쿼리 식 구문

Visual Basic 쿼리 식 구문

추가 정보

Skip

시퀀스에서 지정한 위치까지 요소를 건너뜁니다.

적용할 수 없음

Skip

Enumerable.Skip<TSource>

Queryable.Skip<TSource>

SkipWhile

요소가 조건을 만족하지 않을 때까지 조건자 함수를 기반으로 요소를 건너뜁니다.

적용할 수 없음

Skip While

Enumerable.SkipWhile

Queryable.SkipWhile

Take

시퀀스에서 지정한 위치까지 요소를 취합니다.

적용할 수 없음

Take

Enumerable.Take<TSource>

Queryable.Take<TSource>

TakeWhile

요소가 조건을 만족하지 않을 때까지 조건자 함수를 기반으로 요소를 취합니다.

적용할 수 없음

Take While

Enumerable.TakeWhile

Queryable.TakeWhile

쿼리 식 구문 예제

Skip

다음 코드 예제에서는 Visual Basic의 Skip 절을 사용하여 배열의 나머지 문자열을 반환하기 전에 문자열 배열에서 처음 4개의 문자열을 건너뜁니다.

Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}

Dim query = From word In words _
            Skip 4

Dim sb As New System.Text.StringBuilder()
For Each str As String In query
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' keeps
' the
' doctor
' away

SkipWhile

다음 코드 예제에서는 Visual Basic의 Skip While 절을 사용하여 문자열의 첫 문자가 "a"인 동안 배열에서 문자열을 건너뜁니다. 배열의 나머지 문자열이 반환됩니다.

Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}

Dim query = From word In words _
            Skip While word.Substring(0, 1) = "a"

Dim sb As New System.Text.StringBuilder()
For Each str As String In query
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' day
' keeps
' the
' doctor
' away

Take

다음 코드 예제에서는 Visual Basic의 Take 절을 사용하여 문자열의 배열에서 처음 두 문자열을 반환합니다.

Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}

Dim query = From word In words _
            Take 2

Dim sb As New System.Text.StringBuilder()
For Each str As String In query
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' an
' apple

TakeWhile

다음 코드 예제에서는 Visual Basic의 Take While 절을 사용하여 문자열의 길이가 5이하인 동안 배열에서 문자열을 반환합니다.

Dim words() As String = New String() {"an", "apple", "a", "day", "keeps", "the", "doctor", "away"}

Dim query = From word In words _
            Take While word.Length < 6

Dim sb As New System.Text.StringBuilder()
For Each str As String In query
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' an
' apple
' a
' day
' keeps
' the

참고 항목

개념

표준 쿼리 연산자 개요

참조

Skip 절(Visual Basic)

Skip While 절(Visual Basic)

Take 절(Visual Basic)

Take While 절(Visual Basic)

System.Linq