Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PagedListData&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="pagedListConfig">The paged list config.</param>
        public PagedListData(IQueryable <T> source, PagedListConfig pagedListConfig)
        {
            TotalCount = source.Count();

            PageIndex = pagedListConfig.PageIndex;
            PageSize  = pagedListConfig.PageSize;
            TotalPage = (int)Math.Ceiling(TotalCount / (decimal)PageSize);

            // if source is not already ordered then get order by
            if (!source.IsOrdered())
            {
                OrderBy = pagedListConfig.OrderBy ?? source.ElementType.GetProperties().First(w => !w.GetCustomAttributes(typeof(NotMappedAttribute), true).Any()).Name; // default to first field if no orderby was supplied
                source  = source.OrderBy(OrderBy);
            }

            var queryResult = source.Skip <T>((PageIndex - 1) * PageSize).Take <T>(PageSize).ToList <T>();

            AddRange(queryResult);


            // copy back to pagedListConfig
            pagedListConfig.TotalPage  = TotalPage;
            pagedListConfig.TotalCount = TotalCount;
            pagedListConfig.OrderBy    = OrderBy;
        }
Пример #2
0
        /// <summary>
        /// Convert IQueryable to paged list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The IQueryable source.</param>
        /// <param name="pagedListConfig">The paged list config.</param>
        /// <returns>
        ///   <c>PagedList</c>
        /// </returns>
        public static PagedList <T> ToPagedList <T>(this IQueryable <T> source, PagedListConfig pagedListConfig)
        {
            var pagedList = new PagedListData <T>(source, pagedListConfig);
            var plw       = new PagedList <T> {
                Data = pagedList, Config = pagedListConfig
            };

            return(plw);
        }