次の方法で共有


データのパーティション分割

更新 : 2007 年 11 月

LINQ におけるパーティション分割とは、要素を並べ替えずに入力シーケンスを 2 つのセクションに分割し、それらのセクションの 1 つを返す操作を指します。

次の図は、文字のシーケンスに対して 3 つの異なるパーティション分割操作を実行した結果を示しています。最初の操作では、シーケンスの最初の 3 つの要素が返されます。2 番目の操作では、最初の 3 つの要素がスキップされ、残りの要素が返されます。3 番目の操作では、シーケンスの最初の 2 つの要素がスキップされ、次の 3 つの要素が返されます。

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 句を使用して、文字列配列の最初の 2 つの文字列を返します。

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