LINQ分区操作符

把输入序列分为两个独立的部分,而不重新排列序列中的元素,然后返回其中一个。

操作符 描述 C#查询表达式语法 VB查询表达式语法
Skip 跳过一些指定的序列中一些的元素,并返回其余的 不适用 Skip
SkipWhile 相同,唯一的例外跳到多个元素,跳过的是由一个布尔条件指定 不适用 Skip While
Take 采取元素指定数量的序列,并跳过其余的 不适用 Take
TakeWhile 相同,以不同的事实,即许多元素采取的是由一个布尔条件指定 不适用 Take While

Skip例子- 查询表达式

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

Take例子- 查询表达式

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

上一篇: LINQ量词操作 下一篇: LINQ生成操作