Bagikan melalui


Partitioning Data

Partitioning in LINQ refers to the operation of dividing an input sequence into two sections, without rearranging the elements, and then returning one of the sections.

The following illustration shows the results of three different partitioning operations on a sequence of characters. The first operation returns the first three elements in the sequence. The second operation skips the first three elements and returns the remaining elements. The third operation skips the first two elements in the sequence and returns the next three elements.

LINQ Partitioning Operations

The standard query operator methods that partition sequences are listed in the following section.

Operators

Operator Name

Description

C# Query Expression Syntax

Visual Basic Query Expression Syntax

More Information

Skip

Skips elements up to a specified position in a sequence.

Not applicable.

Skip

Enumerable.Skip<TSource>

Queryable.Skip<TSource>

SkipWhile

Skips elements based on a predicate function until an element does not satisfy the condition.

Not applicable.

Skip While

Enumerable.SkipWhile

Queryable.SkipWhile

Take

Takes elements up to a specified position in a sequence.

Not applicable.

Take

Enumerable.Take<TSource>

Queryable.Take<TSource>

TakeWhile

Takes elements based on a predicate function until an element does not satisfy the condition.

Not applicable.

Take While

Enumerable.TakeWhile

Queryable.TakeWhile

Query Expression Syntax Examples

Skip

The following code example uses the Skip clause in Visual Basic to skip over the first four strings in an array of strings before returning the remaining strings in the array.


        Dim words = {"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

The following code example uses the Skip While clause in Visual Basic to skip over the strings in an array while the first letter of the string is "a". The remaining strings in the array are returned.


        Dim words = {"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

The following code example uses the Take clause in Visual Basic to return the first two strings in an array of strings.


        Dim words = {"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

The following code example uses the Take While clause in Visual Basic to return strings from an array while the length of the string is five or less.


        Dim words = {"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

See Also

Reference

Skip Clause (Visual Basic)

Skip While Clause (Visual Basic)

Take Clause (Visual Basic)

Take While Clause (Visual Basic)

System.Linq

Concepts

Standard Query Operators Overview