Posted by Paul Welter on Thursday, May 31 2007 at 5:48 PM

The following query extension will make paging a query more natural then skip and take. Simply append Paginate(page, pageSize) to your query.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Linq
{
    public static class PageQuery
    {
        public static IQueryable<T> Paginate<T>(
            this IQueryable<T> query, int page, int pageSize)
        {
            int skip = Math.Max(pageSize * (page - 1), 0);
            return query.Skip(skip).Take(pageSize);
        }
    }
}
No Comments Posted in: Snippets    Tagged as: ,

Similar Posts

  1. Fix for the RichTextBox Undo Information Lost When Retrieving Text
  2. XML Serializable Generic Dictionary
  3. Caching the results from LinqDataSource

Comments

Show/Hide Trackbacks
There are no comments yet...

Leave a Comment