Geeks With Blogs
Create Fun Things - Samer's Developer Blog Development Blog for C#, .NET and Obj-C

I’ve read a few tutorials online about paging with LINQ and an ObjectDataSource but there were always quirks that ended up slowing me down unnecessarily so I thought I’d put together my own easy to follow tutorial that should get someone up and running with paging in almost no time!

Step 0: Things I will Assume about your Project

I’ll assume that you have already created a DataContext and dropped in some tables from your database onto it. For my tutorial, I’ll again re-use the “Person” object that I’ve used in previous tutorials. There is really nothing you need to know about it, because my example will work with just about any object/class.

I’ll also assume that you know how to hook a ListView up to an ObjectDataSource, so I won’t spend much time on that when we get there.

If you are unsure how to write custom select methods to hook up to an ObjectDataSource (ODS) then check out my previous tutorial on doing so, because that will build the necessary foundation for these steps.

Step 1:  Write Two Methods in your ClassName.cs file

Remember the partial class I wrote up from before? Here it is again, in case you forgot:


So basically I have a class that allows you to select, update, delete and insert “persons” with an ODS and ListView. I want to now enable paging, because I have thousands of persons and it would be ridiculous to bind them all at once on a page.

I’ll start with the method that gets a record count from the database, because that’s necessary to know how many records to page.

I’ll then write a method that does the actual paging using the Linq methods .Skip() and .Take(). I will also go ahead and mark it with DataObjectMethodType.Select annotation so that the ODS wizard sees it as a Select method automatically.

Here’s everything you’ll need in the class file:


 Update: I realized I made a mistake in the above picture. For the OrderBy to work most effectively (on the entire set of data), you should move the .OrderByDescending statement to right after db.Persons. Otherwise, you will order each individual page, instead of the entire dataset.

If you wanted to, you could using caching or the session to store the count of person records if it doesn’t change often, but for the purposes of this example, I will simply pull it from the database each time.

Before we move on, what’s great about this method of paging is that once it’s hooked up to a paging control, it will only pull record X to Y when the user clicks through each page. In other words, it won’t pull 1000 records, and then page 10 at a time. It will only pull 1-10 ,then 11-20, and so forth. It makes pages load a lot faster, since it wont need to pull unnecessary records.

Step 2: Create a page with a ListView and ObjectDataSource (ODS)

I created a new page, dropped a ListView, and then selected the data source to be an ODS. I chose “Person” as my data object, and then I selected the method I just wrote as the Select method.



On the next page, leave everything blank and hit Finish.


Now, I went to click “configure ListView” and in the left side of the pane, make sure to check “Enable Paging”. Feel free to use whatever style pager you want.



Step 3: Clean up the Source Code To Get Paging to Actually Work

This part is crucial—without doing this little bit of extra work, you won’t get paging to work as intended. Switch to the Source View of your .aspx page ,and find the ObjectDataSource.


The selected part is the part you should delete. The pager will take care of all of that for you.

You’re not done yet! Just two more things for this to work. We’ll need to tell the ODS how to “count” the records, and we’ll need to tell it that we are using Paging in our ListView.

We’ll add “EnablePaging=true” and “SelectCountMethod=”GetRecordCount””. Yours should look something like this once done:



Now you’re ready to build and run the project, and it’ll work!



So as you can see, it’s pretty easy to use paging with LINQ and an ODS. You can definitely get fancier (designing your own pager, caching the record count, etc) but this is the minimum required to get it up and running.

And again, the benefits of this way of paging is that you are only pulling the records as they are requested, which cuts down on page load times and database stress.



samer paul

Posted on Wednesday, July 29, 2009 5:24 PM Tutorials | Back to top

Comments on this post: Tutorial: Paging with LINQ and ObjectDataSource

# re: Tutorial: Paging with LINQ and ObjectDataSource
Requesting Gravatar...
Remember to enable paging on the gridview/datagrid/whatever!
Left by avery on Feb 06, 2013 8:44 AM

Your comment:
 (will show your gravatar)

Copyright © samerpaul | Powered by: