paginationlinq-to-sql

Linq to SQL generic paging method confusion


If i run

ctx.CompanyDirectors.OrderBy(c => c.Id).Skip(pageNumber*pageSize).Take(pageSize).ToList();

The paging happens on SQL Server (the now classic row_number() OVER) and this is as I'd expect.

However I've added a small shortcut extension method below:

public static IEnumerable<T> Page<T>(this IEnumerable<T> enumerable, int pageNumber, int pageSize)
    {
      return enumerable
        .Skip(pageNumber*pageSize)
        .Take(pageSize);
    }

And when i do

ctx.CompanyDirectors.OrderBy(c => c.Id).Page(pageNumber, pageSize).ToList();

The paging now occurs back in the client code i.e. all the results are returned and the paging occurs in memory, which is fugly.

I'm bemused, but am obviously missing something glaringly obvious...


Solution

  • I think you need to have an extension method on IQueryable<T>.