Take 句 (Visual Basic)Take Clause (Visual Basic)
コレクションの先頭から、指定された数の連続する要素を返します。Returns a specified number of contiguous elements from the start of a collection.
構文Syntax
Take count
指定項目Parts
count
必須です。Required. 返されるシーケンスの要素数に評価される値または式。A value or an expression that evaluates to the number of elements of the sequence to return.
RemarksRemarks
Take
句を指定すると、結果リストの先頭から指定した数の連続した要素がクエリに含まれます。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.
Skip
句と共に Take
句を使用すると、クエリの任意のセグメントからのデータの範囲を返すことができます。You can use the Take
clause with the Skip
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. この場合、Take
句を Skip
句の後に指定する必要があります。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.
例Example
次のコード例では、Skip
句と共に Take
句を使用して、ページ内のクエリからのデータを返しています。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)
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