LINQ 中的分区运算符
将输入序列分成两个单独的部分,而无需重新排列序列的元素,然后返回其中一个。
运算符 | 描述 | C# 查询表达式语法 | VB 查询表达式语法 |
---|---|---|---|
Skip | 跳过序列中指定数量的元素并返回剩余元素 | 不适用 | 跳过 |
SkipWhile | 与 Skip 相同,唯一的不同之处在于要跳过的元素数量由布尔条件指定 | 不适用 | Skip While |
Take | 从序列中取出指定数量的元素并跳过剩余的元素 | 不适用 | Take |
TakeWhile | 与 Take 相同,唯一的不同之处在于要取出的元素数量由布尔条件指定 | 不适用 | 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
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