Exemplo n.º 1
0
        public static Page <TSource> ToPage <TSource>(this IList <TSource> source, int totalRecordCount, Paging paging)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var skipCount = PagingExtensions.GetSkipCount(paging);
            var page      = new Page <TSource>
            {
                HasNext          = paging != null && totalRecordCount > 0 && skipCount + paging.PageSize < totalRecordCount,
                HasPrevious      = paging != null && totalRecordCount > 0 && skipCount > 0,
                Items            = source.ToList(),
                PageNumber       = paging?.PageNumber ?? 1,
                RecordsPerPage   = paging?.PageSize ?? 0,
                TotalPageCount   = paging == null ? 1 : paging.PageSize == 0 ? 0 : (int)Math.Ceiling((float)totalRecordCount / (float)paging.PageSize),
                TotalRecordCount = totalRecordCount
            };

            return(page);
        }
Exemplo n.º 2
0
        public static IQueryable <TSource> TakePage <TSource>(this IQueryable <TSource> source, Paging paging)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (paging == null)
            {
                return(source);
            }

            if (paging.SortInfo != null && paging.SortInfo.Any())
            {
                var orderings = string.Join(", ", paging.SortInfo.Select((s, idx) => String.Format("{0} {1}", s.PropertyName, s.Order == SortOrder.Ascending ? "ASC" : "DESC")));

                source = source.OrderBy(orderings);
            }

            return(source.Skip <TSource>(PagingExtensions.GetSkipCount(paging)).Take <TSource>(paging.PageSize));
        }