Last time, I wrote a pager, but now it seems this no longer has use, so I want to paste it here.

    public class Pager<T> where T : new()
    {
        private IEnumerable<T> _all;
        private IEnumerable<T> _current;
        public Pager()
            : this(0, 1000)
        {

        }
        public Pager(int pageIndex, int pageSize)
            : this(null, pageIndex, pageSize)
        {
        }
        public Pager(IEnumerable<T> items, int pageIndex = 0, int pageSize = 10)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            if (items != null)
            {
                _all = items;
                var count = _all.Count();
                PageCount = count % pageSize > 0 ? (count / pageSize + 1) : count / pageSize;
            }
        }


        public int PageIndex { get; set; }
        public int PageSize { get; private set; }
        public int PageCount
        {
            get;
            set;
        }

        public IEnumerable<T> Current
        {
            get
            {
                if (_all != null) _current = _all.Skip(PageIndex * PageSize).Take(PageSize);
                return _current;
            }
            set { _current = value; }
        }

    }

this pager supports dual methods, memory pagination or direct usage.

posted @ 2016-12-08 12:21  calochCN  阅读(299)  评论(0编辑  收藏  举报