public static PageResultHashtable TopageHashTable <TEntity, TResult>(this IQueryable <TEntity> source, Expression <Func <TEntity, bool> > predicate, int pageIndex, int pageSize, Expression <Func <TEntity, TResult> > selector) { int total = source.Count(predicate); source = source.Where(predicate); PageResultHashtable ph = new PageResultHashtable(); List <Hashtable> listht = new List <Hashtable>(); ph.Total = total; if (source != null) { var list = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(selector).ToList(); if (list != null && list.Count > 0) { Type t = list.FirstOrDefault().GetType(); PropertyInfo[] pis = t.GetProperties(); foreach (var item in list) { Hashtable ht = new Hashtable(); foreach (var pi in pis) { object val = pi.GetValue(item, null); ht.Add(pi.Name, val); } listht.Add(ht); } } } ph.Data = listht; return(ph); }
/// <summary> /// 从指定<see cref="IQueryable{T}"/>集合中查询指定数据筛选的分页信息返回带List<Hashtable>集合 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <typeparam name="TResult">分页数据类型</typeparam> /// <param name="source">要查询的数据集</param> /// <param name="predicate">查询条件谓语表达式</param> /// <param name="pageIndex">分页索引</param> /// <param name="pageSize">分页大小</param> /// <param name="sortConditions">排序条件集合</param> /// <param name="selector">数据筛选表达式</param> /// <returns>分页结果信息</returns> public static PageResultHashtable TopageHashTable <TEntity, TResult>(this IQueryable <TEntity> source, Expression <Func <TEntity, bool> > predicate, int pageIndex, int pageSize, SortCondition[] sortConditions, Expression <Func <TEntity, TResult> > selector) { int total = source.Count(predicate); source = source.Where(predicate); if (sortConditions == null || sortConditions.Length == 0) { source = source.OrderBy("Id"); } else { int count = 0; IOrderedQueryable <TEntity> orderSource = null; foreach (SortCondition sortCondition in sortConditions) { orderSource = count == 0 ? CollectionPropertySorter <TEntity> .OrderBy(source, sortCondition.SortField, sortCondition.ListSortDirection) : CollectionPropertySorter <TEntity> .ThenBy(orderSource, sortCondition.SortField, sortCondition.ListSortDirection); count++; } source = orderSource; } PageResultHashtable ph = new PageResultHashtable(); List <Hashtable> listht = new List <Hashtable>(); ph.Total = total; if (source != null) { var list = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(selector).ToList(); if (list != null && list.Count > 0) { Type t = list.FirstOrDefault().GetType(); PropertyInfo[] pis = t.GetProperties(); foreach (var item in list) { Hashtable ht = new Hashtable(); foreach (var pi in pis) { object val = pi.GetValue(item, null); ht.Add(pi.Name, val); } listht.Add(ht); } } } ph.Data = listht; return(ph); }