/// <summary> /// Setting the paging results /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageResult"></param> /// <returns></returns> public virtual PageResultEntity SetPageListResult <T>(PageResultEntity pageResult) { if (pageResult.Data == null) { return(pageResult); } var dataList = pageResult.Data as List <T>; int totalPageIndex = 0; if (dataList.Count == 0) { totalPageIndex = 0; return(pageResult); } else if (pageResult.TotalCount <= pageResult.PageSize) { totalPageIndex = 1; } else { if (pageResult.TotalCount % pageResult.PageSize == 0) { totalPageIndex = pageResult.TotalCount / pageResult.PageSize; } else { totalPageIndex = pageResult.TotalCount / pageResult.PageSize + 1; } } pageResult.TotalPageIndex = totalPageIndex; return(pageResult); }
/// <summary> /// Query paging data based on query fields, filtering conditions, filtering parameters, sorting conditions, number of pages per page, and current page number. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageSize"></param> /// <param name="pageIndex"></param> /// <param name="queryColumns"></param> /// <param name="whereSql"></param> /// <param name="sortCriteria"></param> /// <param name="whereObjParams">whereObjParams,for example:new {Uid=1,Uname="joyet"}</param> /// <returns></returns> public virtual PageResultEntity QueryPageList <T>(int pageSize, int pageIndex, string queryColumns, string whereSql, string sortCriteria, object whereObjParams) { PageResultEntity pageResult = new PageResultEntity(); pageResult.PageSize = pageSize; pageResult.PageIndex = pageIndex; var totalPageDbEntity = SqlBuilder.QueryTotalPageCount <T>(whereSql, whereObjParams); if (totalPageDbEntity == null) { return(pageResult); } var objTotalCount = DbHelper.ExecuteScalar(totalPageDbEntity.CommandText, totalPageDbEntity.DbParams); if (objTotalCount == null) { return(pageResult); } pageResult.TotalCount = Convert.ToInt32(objTotalCount); if (pageResult.TotalCount <= 0) { return(pageResult); } var dbEntity = SqlBuilder.QueryPageList <T>(queryColumns, whereSql, whereObjParams, sortCriteria, pageSize, pageIndex); if (dbEntity == null) { return(pageResult); } List <T> listData; using (var reader = DbHelper.ExecuteReader(dbEntity.CommandText, dbEntity.DbParams)) { listData = DataReaderToEntityList <T>(reader); } pageResult.Data = listData; pageResult = SetPageListResult <T>(pageResult); return(pageResult); }