public static WebPageContainer <T> GeneratePageItems(List <T> items, int eachPageCount, int currentPageNumber)
        {
            WebPageContainer <T> ret = null;

            if (eachPageCount <= 0)
            {
                throw new ArgumentException($"value {eachPageCount} for {nameof(eachPageCount)} is wrong");
            }
            if (items == null)
            {
                throw new ArgumentException($"{nameof(eachPageCount)} is null or empty");
            }
            if (currentPageNumber < 0)
            {
                throw new ArgumentException($"value {currentPageNumber} for {nameof(currentPageNumber)} is less than zero");
            }
            try
            {
                if (currentPageNumber <= 0)
                {
                    currentPageNumber = 1;
                }
                var startIndex = eachPageCount * (currentPageNumber - 1);
                var endIndex   = eachPageCount * currentPageNumber;
                if (endIndex > items.Count())
                {
                    endIndex = items.Count();
                }

                ret = new WebPageContainer <T>()
                {
                    EachPageCount     = eachPageCount,
                    CurrentPageCount  = endIndex - startIndex,
                    Items             = new List <NumberContainer <T> >(),
                    TotalItems        = items.Count,
                    TotalPages        = (int)Math.Ceiling(items.Count / (float)eachPageCount),
                    CurrentPageNumber = currentPageNumber,
                };
                if (items.Count > 0 && ret.TotalPages <= 0)
                {
                    ret.TotalPages = 1;
                }

                ret.OrginItems = new List <T>();
                for (var i = startIndex; i < endIndex; i++)
                {
                    ret.OrginItems.Add(items[i]);
                    ret.Items.Add(new NumberContainer <T>()
                    {
                        Item      = items[i],
                        RowNumber = i
                    });
                }
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
            }
            return(ret);
        }
Exemple #2
0
 public static WebPageContainer <T> ProgressPages <T>(this IEnumerable <T> items, int eachPageCount, int currentPageNumber) where T : class, new()
 => WebPageContainer <T> .GeneratePageItems(items?.ToList(), eachPageCount, currentPageNumber);