I always use the following code:
public static class PagingExtensions
{
//used by LINQ to SQL
public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize)
{
return source.Skip((page - 1) * pageSize).Take(pageSize);
}
//used by LINQ
public static IEnumerable<TSource> Page<TSource>(this IEnumerable<TSource> source, int page, int pageSize)
{
return source.Skip((page - 1) * pageSize).Take(pageSize);
}
}
That is a static class, which you can include in your sources.
After adding this class you can do the following:
MyQuery.Page(pageNumber, pageSize)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…