public static PagedList <T> ToPage <T>(this IOrderedQueryable <T> orderedQuery, int page, int pageSize) { var pagedQuery = orderedQuery as IQueryable <T>; if (page <= 0) { page = 1; } if (page > 1) { pagedQuery = pagedQuery.Skip((page - 1) * pageSize); } pagedQuery = pagedQuery.Take(pageSize); return(new PagedList <T>(pagedQuery.ToList(), page, pageSize, orderedQuery.LongCount())); }