private static void FilterPager(MDataRowCollection rows, int pageIndex, int pageSize) { if (pageIndex > -1 && pageSize > 0) //分页处理返回 { pageIndex = pageIndex == 0 ? 1 : pageIndex; int start = (pageIndex - 1) * pageSize; //从第几条开始查(索引) int end = start + pageSize; //查到第N条结束(索引)。 int rowCount = rows.Count; if (rowCount > end) //总数>N { //rows.re rows.RemoveRange(end, rowCount - end); //移除尾数 } if (start > 0) //总数>起即 { if (rowCount > start) { rows.RemoveRange(0, start);//移除起始数 } else// if (rowCount < start) //查询的超出总数 { rows.Clear();//直接清空返回 } } } }
public static MDataTable Select(MDataTable table, int pageIndex, int pageSize, object whereObj, params object[] selectColumns) { if (table == null) { return(null); } MDataTable sTable = table.GetSchema(true); sTable.RecordsAffected = table.Rows.Count; if (table.Rows.Count == 0) // 正常情况下,看是否需要处理列移除 { FilterColumns(ref sTable, selectColumns); //列查询过滤 return(sTable); } MDataRowCollection findRows = FindAll(table, whereObj); if (findRows != null) { sTable.RecordsAffected = findRows.Count; //设置记录总数 FilterPager(findRows, pageIndex, pageSize); //进行分页筛选,再克隆最后的数据。 for (int i = 0; i < findRows.Count; i++) { if (i < findRows.Count)//内存表时(表有可能在其它线程被清空) { MDataRow row = findRows[i]; if (row == null) { break; } sTable.NewRow(true).LoadFrom(row); } } findRows = null; } if (selectColumns != null && selectColumns.Length > 0) { FilterColumns(ref sTable, selectColumns);//列查询过滤,由于查询的条件可能包含被移除列,所以只能在最后才过滤 } //进行条件查询 return(sTable); }
public static MDataRowCollection FindAll(MDataTable table, object whereObj) { if (table != null && table.Rows.Count > 0) { MDataRowCollection findRows = new MDataRowCollection(); string where = Convert.ToString(whereObj).Trim(); if (where == "" || where == "1=1") { findRows.AddRange(table.Rows); return(findRows); } string whereStr = SqlFormat.GetIFieldSql(whereObj); string orderby; SplitWhereOrderby(ref whereStr, out orderby); List <TFilter> group2 = null; List <TFilter> filters = GetTFilter(whereStr, table.Columns, out group2); IList <MDataRow> rows = table.Rows; if (filters.Count > 0) { rows = table.Rows.FindAll(delegate(MDataRow row) { return(CompareMore(row, filters) && (group2.Count == 0 || CompareMore(row, group2))); } ); } findRows.AddRange(rows); //添加找到的行。 filters = null; if (!string.IsNullOrEmpty(orderby) && rows.Count > 1) //进行数组排序 { findRows.Sort(orderby); //MDataRowCollection sortRows = new MDataRowCollection(); //sortRows.AddRange(rows); //sortRows.Sort(orderby); //return sortRows; } return(findRows); } return(null); }
public static MDataRow FindRow(MDataTable table, object whereObj) { if (table.Rows.Count > 0) { if (Convert.ToString(whereObj).Trim() == "") { return(table.Rows[0]); } string whereStr = SqlFormat.GetIFieldSql(whereObj); string orderby; SplitWhereOrderby(ref whereStr, out orderby); MDataRowCollection sortRows = null; if (!string.IsNullOrEmpty(orderby) && table.Rows.Count > 1)//进行数组排序 { sortRows = new MDataRowCollection(); sortRows.AddRange(table.Rows); sortRows.Sort(orderby); } List <TFilter> group2 = null; List <TFilter> filters = GetTFilter(whereStr, table.Columns, out group2); if (filters.Count > 0) { if (sortRows == null) { sortRows = table.Rows; } for (int i = 0; i < sortRows.Count; i++) { if (CompareMore(sortRows[i], filters) && (group2.Count == 0 || CompareMore(sortRows[i], group2))) { return(sortRows[i]); } } } } return(null); }