Linq-partition-operators

提供:Dev Guides
移動先:案内検索

LINQのパーティションオペレーター

入力シーケンスを2つのセクションに分割します。シーケンスの要素を並べ替えてから1つを返すことはありません。

Operator Description C# Query Expression Syntax VB Query Expression Syntax
Skip Skips some specified number of elements within a sequence and returns the remaining ones Not Applicable Skip
SkipWhile Same as that of Skip with the only exception that number of elements to skip are specified by a Boolean condition Not Applicable Skip While
Take Take a specified number of elements from a sequence and skip the remaining ones Not Applicable Take
TakeWhile Same as that of Take except the fact that number of elements to take are specified by a Boolean condition Not Applicable Take While

スキップの例-クエリ式

VB

Module Module1

   Sub Main()

      Dim words = {"once", "upon", "a", "time", "there", "was", "a", "jungle"}

      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)
         Console.WriteLine(str)
      Next

      Console.ReadLine()

   End Sub

End Module

VBの上記のコードがコンパイルされて実行されると、次の結果が生成されます-

there
was
a
jungle

Skip Whileの例-クエリ式

VB

Module Module1

   Sub Main()

      Dim words = {"once", "upon", "a", "time", "there", "was", "a", "jungle"}

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

      Dim sb As New System.Text.StringBuilder()

      For Each str As String In query
         sb.AppendLine(str)
         Console.WriteLine(str)
      Next

      Console.ReadLine()
   End Sub

End Module

VBの上記のコードがコンパイルされて実行されると、次の結果が生成されます-

once
upon
a
was
a
jungle

テイクの例-クエリ式

VB

Module Module1

   Sub Main()

      Dim words = {"once", "upon", "a", "time", "there", "was", "a", "jungle"}

      Dim query = From word In words
                  Take 3

      Dim sb As New System.Text.StringBuilder()

      For Each str As String In query
         sb.AppendLine(str)
         Console.WriteLine(str)
      Next

      Console.ReadLine()

   End Sub

End Module

VBの上記のコードがコンパイルされて実行されると、次の結果が生成されます-

once
upon
a

Take Whileの例-クエリ式

VB

Module Module1

   Sub Main()

      Dim words = {"once", "upon", "a", "time", "there", "was", "a", "jungle"}

      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)
         Console.WriteLine(str)
      Next

      Console.ReadLine()

   End Sub

End Module

VBの上記のコードがコンパイルされて実行されると、次の結果が生成されます-

once
upon
a
time
there
was
a