Skip to main content

LINQ To SQL Samples - Paging


Paging - Index

This sample uses the Skip and Take operators to do paging by skipping the first 50 records and then returning the next 10, thereby providing the data for page 6 of the Products table.

Public Sub LinqToSqlPaging01()
    Dim q = From c In db.Customers _
            Order By c.ContactName Skip 50 Take 10

    ObjectDumper.Write(q)
End Sub

Result:
CustomerID=THECR        CompanyName=The Cracker Box     ContactName=Liu Wong    ContactTitle=Marketing Assistant        Address=55 Grizzly Peak Rd.     City=Butte      Region=MT       PostalCode=59801        Country=USA     Phone=(406) 555-5834    Fax=(406) 555-8083      Orders=...      CustomerCustomerDemos=...
CustomerID=THEBI        CompanyName=The Big Cheese      ContactName=Liz Nixon   ContactTitle=Marketing Manager  Address=89 Jefferson Way Suite 2        City=Portland   Region=OR       PostalCode=97201        Country=USA     Phone=(503) 555-3612    Fax=null        Orders=...      CustomerCustomerDemos=...
CustomerID=QUEEN        CompanyName=Queen Cozinha       ContactName=Lúcia Carvalho      ContactTitle=Marketing Assistant        Address=Alameda dos Canàrios, 891       City=Sao Paulo  Region=SP       PostalCode=05487-020    Country=Brazil  Phone=(11) 555-1189     Fax=null        Orders=...      CustomerCustomerDemos=...
CustomerID=GROSR        CompanyName=GROSELLA-Restaurante        ContactName=Manuel Pereira      ContactTitle=Owner      Address=5ª Ave. Los Palos Grandes       City=Caracas    Region=DF       PostalCode=1081         Country=Venezuela       Phone=(2) 283-2951      Fax=(2) 283-3397        Orders=...      CustomerCustomerDemos=...
CustomerID=ALFKI        CompanyName=Alfreds Futterkiste         ContactName=Maria Anders        ContactTitle=Sales Representative       Address=Obere Str. 57   City=Berlin     Region=null     PostalCode=12209        Country=Germany         Phone=030-0074321       Fax=030-0076545         Orders=...      CustomerCustomerDemos=...
CustomerID=FOLKO        CompanyName=Folk och fä HB      ContactName=Maria Larsson       ContactTitle=Owner      Address=Åkergatan 24    City=Bräcke     Region=null     PostalCode=S-844 67     Country=Sweden  Phone=0695-34 67 21     Fax=null        Orders=...      CustomerCustomerDemos=...
CustomerID=PARIS        CompanyName=Paris spécialités   ContactName=Marie Bertrand      ContactTitle=Owner      Address=265, boulevard Charonne         City=Paris      Region=null     PostalCode=75012        Country=France  Phone=(1) 42.34.22.66   Fax=(1) 42.34.22.77     Orders=...      CustomerCustomerDemos=...
CustomerID=HANAR        CompanyName=Hanari Carnes       ContactName=Mario Pontes        ContactTitle=Accounting Manager         Address=Rua do Paço, 67         City=Rio de Janeiro     Region=RJ       PostalCode=05454-876    Country=Brazil  Phone=(21) 555-0091     Fax=(21) 555-8765       Orders=...      CustomerCustomerDemos=...
CustomerID=BOLID        CompanyName=Bólido Comidas preparadas   ContactName=Martín Sommer       ContactTitle=Owner      Address=C/ Araquil, 67  City=Madrid     Region=null     PostalCode=28023        Country=Spain   Phone=(91) 555 22 82    Fax=(91) 555 91 99      Orders=...      CustomerCustomerDemos=...
CustomerID=FOLIG        CompanyName=Folies gourmandes   ContactName=Martine Rancé       ContactTitle=Assistant Sales Agent      Address=184, chaussée de Tournai        City=Lille      Region=null     PostalCode=59000        Country=France  Phone=20.16.10.16       Fax=20.16.10.17         Orders=...      CustomerCustomerDemos=...


Paging - Ordered Unique Key

This sample uses a where clause and the Take operator to do paging by, first filtering to get only the ProductIDs above 50 (the last ProductID from page 5), then ordering by ProductID, and finally taking the first 10 results, thereby providing the data for page 6 of the Products table. Note that this method only works when ordering by a unique key.

Public Sub LinqToSqlPaging02()
    Dim q = From p In db.Products _
            Where p.ProductID > 50 _
            Select p _
            Order By p.ProductID Take 10

    ObjectDumper.Write(q)
End Sub

Result:
ProductID=51    ProductName=Manjimup Dried Apples       SupplierID=24   CategoryID=7    QuantityPerUnit=50 - 300 g pkgs.        UnitPrice=54.0000       UnitsInStock=20         UnitsOnOrder=0  ReorderLevel=10         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=52    ProductName=Filo Mix    SupplierID=24   CategoryID=5    QuantityPerUnit=16 - 2 kg boxes         UnitPrice=8.0000        UnitsInStock=38         UnitsOnOrder=0  ReorderLevel=25         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=53    ProductName=Perth Pasties       SupplierID=24   CategoryID=6    QuantityPerUnit=48 pieces       UnitPrice=33.8000       UnitsInStock=0  UnitsOnOrder=0  ReorderLevel=0  Discontinued=True       Order_Details=...       Category={ }    Supplier={ }
ProductID=54    ProductName=Tourtière   SupplierID=25   CategoryID=6    QuantityPerUnit=16 pies         UnitPrice=8.4500        UnitsInStock=21         UnitsOnOrder=0  ReorderLevel=10         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=55    ProductName=Pâté chinois        SupplierID=25   CategoryID=6    QuantityPerUnit=24 boxes x 2 pies       UnitPrice=25.0000       UnitsInStock=115        UnitsOnOrder=0  ReorderLevel=20         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=56    ProductName=Gnocchi di nonna Alice      SupplierID=26   CategoryID=5    QuantityPerUnit=24 - 250 g pkgs.        UnitPrice=39.0000       UnitsInStock=21         UnitsOnOrder=10         ReorderLevel=30         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=57    ProductName=Ravioli Angelo      SupplierID=26   CategoryID=5    QuantityPerUnit=24 - 250 g pkgs.        UnitPrice=20.5000       UnitsInStock=36         UnitsOnOrder=0  ReorderLevel=20         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=58    ProductName=Escargots de Bourgogne      SupplierID=27   CategoryID=8    QuantityPerUnit=24 pieces       UnitPrice=14.2500       UnitsInStock=62         UnitsOnOrder=0  ReorderLevel=20         Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=59    ProductName=Raclette Courdavault        SupplierID=28   CategoryID=4    QuantityPerUnit=5 kg pkg.       UnitPrice=56.0000       UnitsInStock=79         UnitsOnOrder=0  ReorderLevel=0  Discontinued=False      Order_Details=...       Category={ }    Supplier={ }
ProductID=60    ProductName=Camembert Pierrot   SupplierID=28   CategoryID=4    QuantityPerUnit=15 - 300 g rounds       UnitPrice=35.0000       UnitsInStock=19         UnitsOnOrder=0  ReorderLevel=0  Discontinued=False      Order_Details=...       Category={ }    Supplier={ }