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

略過集合中指定數目的項目,然後傳回其餘項目。Bypasses a specified number of elements in a collection and then returns the remaining elements.


Skip count  


必要項。Required. 值或評估運算式,以略過序列的項目數目。A value or an expression that evaluates to the number of elements of the sequence to skip.


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.


下列程式碼範例會使用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)


    pageNumber += 1
    customersPage = GetCustomers(pageNumber * pageSize, pageSize)
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