Freigeben über


Skip Clause (Visual Basic)

Bypasses a specified number of elements in a collection and then returns the remaining elements.

Skip count

Parts

  • count
    Required. A value or an expression that evaluates to the number of elements of the sequence to skip.

Remarks

The Skip clause causes a query to bypass elements at the beginning of a results list and return the remaining elements. The number of elements to skip is identified by the count parameter.

You can use the Skip clause with the Take clause to return a range of data from any segment of a query. 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.

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. For more information about ordering query results, see Order By Clause (Visual Basic).

You can use the SkipWhile clause to specify that only certain elements are ignored, depending on a supplied condition.

Example

The following code example uses the Skip clause together with the Take clause to return data from a query in pages. 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

Reference

Select Clause (Visual Basic)

From Clause (Visual Basic)

Order By Clause (Visual Basic)

Skip While Clause (Visual Basic)

Take Clause (Visual Basic)

Concepts

Introduction to LINQ in Visual Basic

Other Resources

Queries (Visual Basic)