Share via


分割資料

LINQ 中的分割作業是指在不重新排列項目的情況下將輸入序列分成兩個區段,然後傳回其中一個區段。

下圖顯示對某個字元序列執行三種不同分割作業的結果。 第一個作業會傳回序列中的前三個項目。 第二個作業會略過前三個項目,並傳回其餘的項目。 第三個作業則會略過序列中的前兩個項目,並傳回接下來的三個項目。

LINQ 分割作業

下節會列出分割序列的標準查詢運算子方法。

運算子

運算子名稱

描述

C# 查詢運算式語法

Visual Basic 查詢運算式語法

詳細資訊

Skip

略過序列中的項目,直到指定的位置為止。

不適用。

Skip

Enumerable.Skip<TSource>

Queryable.Skip<TSource>

SkipWhile

根據述詞 (Predicate) 函式略過項目,直到項目不符合條件為止。

不適用。

Skip While

Enumerable.SkipWhile

Queryable.SkipWhile

Take

取用佇列中的項目,直到指定的位置為止。

不適用。

Take

Enumerable.Take<TSource>

Queryable.Take<TSource>

TakeWhile

根據述詞函式取用項目,直到項目不符合條件為止。

不適用。

Take While

Enumerable.TakeWhile

Queryable.TakeWhile

查詢運算式語法範例

Bb546164.collapse_all(zh-tw,VS.110).gifSkip

下列程式碼範例使用 Visual Basic 中的 Skip 子句,略過字串陣列中的前四個字串,再傳回陣列中的其餘字串。


        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

Bb546164.collapse_all(zh-tw,VS.110).gifSkipWhile

下列程式碼範例使用 Visual Basic 中的 Skip While 子句,略過陣列中字串第一個字母是 "a" 的字串。 然後傳回陣列中的其餘字串。


        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

Bb546164.collapse_all(zh-tw,VS.110).gifTake

下列程式碼範例使用 Visual Basic 中的 Take 子句,傳回字串陣列中的前兩個字串。


        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

Bb546164.collapse_all(zh-tw,VS.110).gifTakeWhile

下列程式碼範例使用 Visual Basic 中的 Take While 子句,傳回陣列中字串長度是 5 或以下的字串。


        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

請參閱

參考

Skip 子句 (Visual Basic)

Skip While 子句 (Visual Basic)

Take 子句 (Visual Basic)

Take While 子句 (Visual Basic)

System.Linq

概念

標準查詢運算子概觀