Pagination on Linq to SQL

by OfirYaron 3/15/2009 5:49:00 AM

Paging on linq is so much easier then anything I've ever worked with.

consider the following code to add a generic GetPage method in case you use it on several Data Entities in your application:

public static T[] GetPage<T, K>(System.Data.Linq.Table<T> tableObject,

System.Linq.Expressions.Expression<Func<T, K>> orderPred,

int pageSize, int selectedPage) where T : class, new()

{

return tableObject.OrderByDescending(orderPred).Skip(pageSize * (selectedPage - 1)).Take(pageSize).ToArray();

}

public static void Test()

{

// Say this is my Users table:

//User(1,"Ofir")

//User(2,"Yaron")

//User(3,"Danny")

//User(4,"Gila")

//User(5,"Ran")

//User(6,"John")

//User(7,"Marry")

//User(8,"Shay")

//User(9,"Shula")

//User(10,"Moish")

//getting the second page - 4 objects

User[] point = GetPage<User, int>(Utilities.DB.Users, u => u.Id, 4, 2);

//returns:

//Ran

//John

//Marry

//Shay

}

 

Currently rated 4.5 by 4 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Related posts

Comments are closed

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in