Take 子句 (Visual Basic)Take Clause (Visual Basic)

从集合的开头返回指定数量的连续元素。Returns a specified number of contiguous elements from the start of a collection.


Take count  


必需。Required. 值或计算结果为要返回的序列的元素数的表达式。A value or an expression that evaluates to the number of elements of the sequence to return.


@No__t-0 子句使查询包括从结果列表的开头开始的指定数量的连续元素。The Take clause causes a query to include a specified number of contiguous elements from the start of a results list. 要包括的元素数由 count 参数指定。The number of elements to include is specified by the count parameter.

可以将 Take 子句与 Skip 子句一起使用,以从查询的任何段返回数据范围。You can use the Take clause with the Skip clause to return a range of data from any segment of a query. 为此,请将范围的第一个元素的索引传递到 Skip 子句,并将范围的大小传递到 @no__t 的子句。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. 在这种情况下,必须在 @no__t 1 子句之后指定 Take 子句。In this case, the Take clause must be specified after the Skip clause.

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

您可以使用 TakeWhile 子句来指定仅返回某些元素,具体取决于所提供的条件。You can use the TakeWhile clause to specify that only certain elements be returned, depending on a supplied condition.


下面的代码示例将 Take 子句与 @no__t 子句一起使用,以便在页中从查询返回数据。The following code example uses the Take clause together with the Skip 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