Skip 子句 (Visual Basic)Skip Clause (Visual Basic)

绕过集合中指定数量的元素,然后返回剩余的元素。Bypasses a specified number of elements in a collection and then returns the remaining elements.

语法Syntax

Skip count  

部件Parts

count
必需。Required. 一个值或表达式的计算结果为要跳过序列中的元素数。A value or an expression that evaluates to the number of elements of the sequence to skip.

备注Remarks

Skip子句会使查询以绕过在结果列表的开始处的元素并返回剩余元素。The Skip clause causes a query to bypass elements at the beginning of a results list and return the remaining elements. 要跳过的元素数由count参数。The number of elements to skip is identified by the count parameter.

可以使用Skip子句与Take子句从查询的任何段返回一系列数据。You can use the Skip clause with the Take clause to return a range of data from any segment of a query. 若要执行此操作,将传递到范围内的第一个元素的索引Skip子句和范围的大小Take子句。To do this, pass the index of the first element of the range to the Skip clause and the size of the range to the Take clause.

当你使用Skip查询中的子句,您可能还需要确保将启用的顺序返回结果Skip子句,以绕过预期的结果。When you use the Skip clause in a query, you may also need to ensure that the results are returned in an order that will enable the Skip clause to bypass the intended results. 对查询结果进行排序的详细信息,请参阅Order By 子句For more information about ordering query results, see Order By Clause.

可以使用SkipWhile子句来指定只有某些元素将忽略,具体取决于提供的条件。You can use the SkipWhile clause to specify that only certain elements are ignored, depending on a supplied condition.

示例Example

下面的代码示例使用Skip子句一起使用Take子句,以从页中的查询返回的数据。The following code example uses the Skip clause together with the Take clause to return data from a query in pages. GetCustomers函数使用Skip子句,以跳过列表中的客户,直到提供的起始索引值,并使用Take子句返回的客户从该索引值开始页。The GetCustomers function uses the Skip clause to bypass the customers in the list until the supplied starting index value, and uses the Take clause to return a page of customers starting from that index value.

Public Sub PagingSample()
  Dim pageNumber As Integer = 0
  Dim pageSize As Integer = 10

  Dim customersPage = GetCustomers(pageNumber * pageSize, pageSize)

  Do While customersPage IsNot Nothing
    Console.WriteLine(vbCrLf & "Page: " & pageNumber + 1 & vbCrLf)

    For Each cust In customersPage
      Console.WriteLine(cust.CustomerID & ", " & cust.CompanyName)
    Next

    Console.WriteLine(vbCrLf)

    pageNumber += 1
    customersPage = GetCustomers(pageNumber * pageSize, pageSize)
  Loop
End Sub

Public Function GetCustomers(ByVal startIndex As Integer,
                             ByVal pageSize As Integer) As List(Of Customer)

  Dim customers = GetCustomerList()

  Dim returnCustomers = From cust In customers
                        Skip startIndex Take pageSize

  If returnCustomers.Count = 0 Then Return Nothing

  Return returnCustomers
End Function

请参阅See also