public static PagedList <T> ToPageList <T>(this IContentQuery <T> contentQuery, int pageIndex, int pageSize, bool enablePaging = true) where T : ContentBase { if (enablePaging == true) { if (pageIndex < 1) { pageIndex = 1; } var itemIndex = (pageIndex - 1) * pageSize; var totalItemCount = contentQuery.Count(); var pageOfItems = contentQuery.Skip(itemIndex).Take(pageSize); return(new PagedList <T>(pageOfItems, pageIndex, pageSize, totalItemCount)); } else { var totalItemCount = contentQuery.Count(); var pageOfItems = contentQuery.Take(totalItemCount); return(new PagedList <T>(pageOfItems, 1, totalItemCount, totalItemCount)); } }