C#数据集合分页处理

 1:集合进行分页


     ///
<summary> /// 分页处理 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="item"></param> /// <param name="pageSize"></param> /// <param name="singlePageList"></param> protected void ToPagingProcess<TEntity>(IEnumerable<TEntity> item, int pageSize, Action<IEnumerable<TEntity>> singlePageList) { if (item != null && item.Count() > 0) { var count = item.Count(); var pages = item.Count() / pageSize; if (count % pageSize > 0) { pages += 1; } for (int i = 1; i <= pages; i++) { var currentPageItem = item.Skip((i - 1) * pageSize).Take(pageSize); singlePageList(currentPageItem); } } }
    注意:特殊情况下会存在数据重复的情况出现

 2:SQL进行分页

    /// <summary>
    /// 用于显示列表的公共参数
    /// 无论前台页面用何种Grid,这里做为系统Grid参数名不会变
    /// </summary>
    public class GridParameter
    {
        /// <summary>
        /// 当前页
        /// </summary>
        public int Page { get; set; }

        /// <summary>
        /// 页总数
        /// </summary>
        public int TotalPages { get; set; }

        /// <summary>
        /// 行总数
        /// </summary>
        public int Records { get; set; }

        /// <summary>
        /// 每页显示的行数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 排序列名
        /// </summary>
        public string SortName { get; set; }

        /// <summary>
        /// 排序方式(asc/desc)
        /// </summary>
        public string SortOrder { get; set; }

        /// <summary>
        /// 不排序
        /// </summary>
        /// <remarks>
        /// 这个问题也可以解决,可排序前判断 q.Expression.ToString() 中是否含有".OrderBy("或".OrderByDescending("
        /// </remarks>
        public bool NotSort { get; set; }

        /// <summary>
        /// 搜索内容
        /// </summary>
        public string SearchText { get; set; }

        /// <summary>
        /// 搜索字段。
        /// 在多字段搜索里不用此属性
        /// </summary>
        public string SearchField { get; set; }

        /// <summary>
        /// 用户数据。通常配合统计结果,存入json数据。
        /// </summary>
        public object UserData { get; set; }
 
        private string _statsSign = null;
        /// <summary>
        /// 统计标识,配合Stats属性
        /// </summary>
        public string StatsSign
        {
            get { return _statsSign; }
            set
            {
                _statsSign = value;
                if (!string.IsNullOrEmpty(_statsSign) && Stats == null)
                {
                    Stats = new Dictionary<object, object>();
                }
            }
        }
        /// <summary>
        /// 统计列表
        /// </summary>
        public Dictionary<object, object> Stats { get; set; }

        public GridParameter() { }
        public GridParameter(int page, int pageSize, string sortName, string sortOrder, string searchText)
        {
            this.Page = page;
            this.PageSize = pageSize;
            this.SortName = sortName;
            this.SortOrder = sortOrder;
            this.SearchText = searchText;
        }
    }
    protected List<T> GetPagedList<T>(string sql, GridParameter gp)
        {
            if (gp.Page <= 0) gp.Page = Defaults.GridPage;
            if (gp.PageSize <= 0) gp.PageSize = Defaults.GridPageSize;
            var endNum = (gp.Page - 1) * gp.PageSize;//分页结束位置
            var startNum = (gp.Page - 1) * gp.PageSize + gp.PageSize;//分页开始位置
            string orderBy = "";
            //排序
            if (!string.IsNullOrEmpty(gp.SortName))
            {
                orderBy += " ROW_NUMBER() OVER(ORDER BY " + gp.SortName + " " + gp.SortOrder + ")";
            }
            else
            {
                orderBy += " ROWNUM";
            }
            //分页SQL
            string sqlPage = string.Format("SELECT * FROM(SELECT TABLE1.*,{3} RN FROM({0}) TABLE1) WHERE RN<={1} AND RN>{2}", sql, startNum, endNum, orderBy);
            //查询总数SQL
            string sqlTotal = string.Format("SELECT COUNT(1) FROM ({0})", sql);
            gp.Records = ObjContext.ExecuteStoreQuery<int?>(sqlTotal).FirstOrDefault() ?? 0;
            //执行SQL
            return ObjContext.ExecuteStoreQuery<T>(sqlPage, null).ToList();
        }

 

posted @ 2018-03-13 16:35  木头马尾、、、  阅读(2975)  评论(0编辑  收藏  举报