Web developers use optional LINQ Take clause when they want to include in a query a specified number of contiguous elements from the start of a result list which is specified by the count parameter:
Take countwhere count is required value or an expression that evaluates to the number of elements of the sequence to return.
The Take clause can be used with the Skip clause to return a range of data from any segment of a query. Web developers can do this, by passing the index of the first element of the range to Skip clause and the size of the range to the Take clause. In this case, the Take clause must be specified after the Skip clause.
When Web developers use the Take clause in a query, he/she needs to ensure that the results are returned in an order that will enable the Take clause to include the intended results.
The next code example uses the combination of Skip and Take clause to return data from a query in pages. The GetProducts function uses the Skip clause to bypass the products in the list until the supplied starting index value, and uses the Take clause to return a page of products starting from that index value:
Public Sub PagingProducts()
Dim pgNum As Integer = 0
Dim pgSz As Integer = 15
Dim productsPg = GetProducts(pgNum*pgSz, pgSz)
Do While producsPg IsNot Nothing
Console.WriteLine(vbCrLf & “Page: ” & pgNum + 1 & vbCrLf)
For Each prd In productsPg
Console.WriteLine(prd.ProductID & “, ” & prd.Company)
pgNum = pgNum + 1
productsPg = GetProducts(pgNum*pgSz, pgSz)
Public Function GetProducts(ByVal begIndex as Integer, _Your hosting company should support SQL 2008 hosting to host ASP.NET with MS SQL 2008.
ByVal pgSz as Integer) as List (Of Product)
Dim products = GetProductList()
Dim result = From prod In products _
Skip begIndex Take pgSz
If result.Count = 0 Then Return Nothing
Return resultEnd Function