Liams blog RSS Feed


Linq skip and take for paging
I have recently had the chance to play around with Linq to SQL in Visual Studio 2008 Beta 2. It provides a really quick and easy way of getting data out of a SQL database and into Model objects. One of the challenges I have faced is with regard to the amount of data rows returned from an underlying table. It is a common scenario on many web sites to implement a paging feature where the web site will render a small amount of products (say 20), then provide previous and next buttons to see more results. This is where the .Skip and .Take methods come into play. .skip is used to determine how many rows to skip and .Take is used to determine how many rows to return.   The code sample below will return the first 20 Products from the Product Table. To get the next 20 we would set Skip to 20 and leave take at 20.   ProductDataContext db = new ProductDataContext(); var Products =    from p in db.Products select p;   foreach (FrontEndWeb.Linq.Product p in Products.Skip(0).Take(20)) {       Response.Write(p.Name + "
"); }          
 
Posted by Liam Molloy | 0 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation