標籤:return 根據 query sea new null 返回 etc data
返回的是list集合:
/// <summary> /// 返回合約的款項資訊 /// </summary> /// <param name="pagesize"></param> /// <param name="currentpage"></param> /// <param name="totalcount"></param> /// <returns></returns> public List<Contract.Model.MoneyLog> GetContractMoneyLogs(int pagesize, int currentpage, out int totalcount) { try { totalcount = 0; if (this.ContractID == 0) throw new ContractException("合約編號不存在"); List<Contract.Model.MoneyLog> listm = CClient.GetContractMoneyLogList(this.ContractID); if (listm != null) { totalcount = listm.Count; //記錄日誌 string msg = string.Format("查詢合約款項資訊 記錄數:{0}", listm.Count); Model.ContractLog L = new ContractLog(this.ContractID, msg, this.UserID); CClient.AddContractLogToDB(L); //對返回的listm集合進行分頁操作 return ((List<Contract.Model.MoneyLog>)FilterList(pagesize, currentpage, listm)); } return null; } catch (Exception ex) { throw ex; } finally { CClient.Close(); } }
/// <summary> /// 根據總條數和當前頁,返回當前頁的list /// </summary> /// <param name="pagesize"></param> /// <param name="currentpage"></param> /// <param name="list"></param> /// <returns></returns> private List<T> FilterList<T>(int pagesize, int currentpage, List<T> list) { try { int totalcount = list.Count; if (totalcount <= pagesize) return list; int totalpage = totalcount / pagesize + ((totalcount % pagesize == 0) ? 0 : 1); int querysize = pagesize; //如果是最後一頁,需要計算一下最後剩餘的記錄條數 if (currentpage >= totalpage) querysize = (totalcount % pagesize == 0) ? pagesize : totalcount % pagesize; return list.GetRange((currentpage - 1) * pagesize, querysize); } catch (Exception ex) { throw ex; } }
返回的是DataTable
/// <summary> /// 擷取待審批或已審批合約列表,此方法全部由王君添加 /// </summary> /// <param name="userid">當前登入帳號的 userid</param> /// <param name="auditstate">審核狀態:未審 0,已審 1</param> /// <returns></returns> public DataTable SearchAuditContracts(int auditstate, int pageIndex, int pageSize, out int totalcount) { try { totalcount = 0; DataTable results = CClient.SearchAuditContracts(this.UserID, auditstate); if (results.Rows.Count > 0) { //記錄日誌 string msg = string.Format("查詢合約審批列表資訊 記錄數:{0}", results.Rows.Count); Model.ContractLog L = new ContractLog(this.ContractID, msg, this.UserID); CClient.AddContractLogToDB(L); totalcount = results.Rows.Count; return Utility.SplitDataTable(results, pageIndex, pageSize); } return results; } catch (Exception ex) { throw ex; } finally { CClient.Close(); } }
/// <summary> /// 根據索引和pagesize返回記錄 /// </summary> /// <param name="dt">記錄集 DataTable</param> /// <param name="PageIndex">當前頁</param> /// <param name="pagesize">一頁的記錄數,此方法全部由王君添加</param> /// <returns></returns> public static DataTable SplitDataTable(DataTable dt, int PageIndex, int PageSize) { int totalcount = dt.Rows.Count; if (PageIndex == 0) return dt; DataTable newdt = dt.Clone(); //newdt.Clear(); int rowbegin = (PageIndex - 1) * PageSize; int rowend = PageIndex * PageSize; if (rowbegin >= dt.Rows.Count) return newdt; if (rowend > dt.Rows.Count) rowend = dt.Rows.Count; for (int i = rowbegin; i <= rowend - 1; i++) { DataRow newdr = newdt.NewRow(); DataRow dr = dt.Rows[i]; foreach (DataColumn column in dt.Columns) { newdr[column.ColumnName] = dr[column.ColumnName]; } newdt.Rows.Add(newdr); } return newdt; }
c# 分頁的方法