public virtual CPPagedResult <TEntity> GetByCondition( ISpecification <TEntity> specification, Expression <Func <TEntity, dynamic> > orderBy, CPEFEnum.SortOrder orderType, int pageSize, int currentPage , params Expression <Func <TEntity, dynamic> >[] eagerLoadingProperties) { DbSet <TEntity> dbSet = this._dbContext.Set <TEntity>(); IQueryable <TEntity> queryable = null;// this._dbContext.Set<TEntity>().Where(specification.GetExpression()); if (eagerLoadingProperties != null && eagerLoadingProperties.Length > 0) { var eagerLoadingProperty = eagerLoadingProperties[0]; var eagerLoadingPath = this.GetEagerLoadingPath(eagerLoadingProperty); var dbquery = dbSet.Include(eagerLoadingPath); for (int i = 1; i < eagerLoadingProperties.Length; i++) { eagerLoadingProperty = eagerLoadingProperties[i]; eagerLoadingPath = this.GetEagerLoadingPath(eagerLoadingProperty); dbquery = dbquery.Include(eagerLoadingPath); } queryable = dbquery.Where(specification.GetExpression()); } else { queryable = dbSet.Where(specification.GetExpression()); } IQueryable <TEntity> queryableData = this._dbContext.Set <TEntity>().Where(specification.GetExpression()); if (orderType == CPEFEnum.SortOrder.Asc) { queryableData = queryable.SortBy(orderBy); } else { queryableData = queryable.SortByDescending(orderBy); } queryableData = queryableData.Skip(currentPage * (currentPage - 1)) //跳过行数,最终生成的sql语句是Top(n) .Take(pageSize).AsQueryable(); //返回指定数量的行 int nCount = queryable.Count(); //获取总记录数 List <TEntity> col = queryableData.ToList(); return(new CPPagedResult <TEntity>(nCount, (nCount + pageSize - 1) / pageSize, pageSize, currentPage, col)); }
private static string GetSortingMethodName(CPEFEnum.SortOrder sortOrder) { switch (sortOrder) { case CPEFEnum.SortOrder.Asc: return("OrderBy"); case CPEFEnum.SortOrder.Desc: return("OrderByDescending"); default: throw new ArgumentException("Sort Order must be specified as either Ascending or Descending.", "sortOrder"); } }
public virtual IList <TEntity> GetByCondition(ISpecification <TEntity> specification, Expression <Func <TEntity, dynamic> > orderBy, CPEFEnum.SortOrder orderType , params Expression <Func <TEntity, dynamic> >[] eagerLoadingProperties) { DbSet <TEntity> dbSet = this._dbContext.Set <TEntity>(); IQueryable <TEntity> queryable = null; //this._dbContext.Set<TEntity>().Where(specification.GetExpression()); if (eagerLoadingProperties != null && eagerLoadingProperties.Length > 0) { var eagerLoadingProperty = eagerLoadingProperties[0]; var eagerLoadingPath = this.GetEagerLoadingPath(eagerLoadingProperty); var dbquery = dbSet.Include(eagerLoadingPath); for (int i = 1; i < eagerLoadingProperties.Length; i++) { eagerLoadingProperty = eagerLoadingProperties[i]; eagerLoadingPath = this.GetEagerLoadingPath(eagerLoadingProperty); dbquery = dbquery.Include(eagerLoadingPath); } queryable = dbquery.Where(specification.GetExpression()); } else { queryable = dbSet.Where(specification.GetExpression()); } if (orderBy != null) { if (orderType == CPEFEnum.SortOrder.Asc) { queryable = queryable.SortBy(orderBy); } else { queryable = queryable.SortByDescending(orderBy); } } //string Sql = this.GetTraceString<TEntity>(queryable); // List <TEntity> col = queryable.ToList(); col.ToList().ForEach(t => { t.FormatInitValue(); }); return(col); }
private static IOrderedQueryable <TEntity> InvokeSortBy <TEntity>(IQueryable <TEntity> query, Expression <Func <TEntity, dynamic> > sortPredicate, CPEFEnum.SortOrder sortOrder) where TEntity : class, IEntity { var param = sortPredicate.Parameters[0]; string propertyName = null; Type propertyType = null; Expression bodyExpression = null; if (sortPredicate.Body is UnaryExpression) { UnaryExpression unaryExpression = sortPredicate.Body as UnaryExpression; bodyExpression = unaryExpression.Operand; } else if (sortPredicate.Body is MemberExpression) { bodyExpression = sortPredicate.Body; } else { throw new ArgumentException(@"The body of the sort predicate expression should be either UnaryExpression or MemberExpression.", "sortPredicate"); } MemberExpression memberExpression = (MemberExpression)bodyExpression; propertyName = memberExpression.Member.Name; if (memberExpression.Member.MemberType == MemberTypes.Property) { PropertyInfo propertyInfo = memberExpression.Member as PropertyInfo; propertyType = propertyInfo.PropertyType; } else { throw new InvalidOperationException(@"Cannot evaluate the type of property since the member expression represented by the sort predicate expression does not contain a PropertyInfo object."); } Type funcType = typeof(Func <,>).MakeGenericType(typeof(TEntity), propertyType); LambdaExpression convertedExpression = Expression.Lambda(funcType, Expression.Convert(Expression.Property(param, propertyName), propertyType), param); var sortingMethods = typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static); var sortingMethodName = GetSortingMethodName(sortOrder); var sortingMethod = sortingMethods.Where(sm => sm.Name == sortingMethodName && sm.GetParameters() != null && sm.GetParameters().Length == 2).First(); return((IOrderedQueryable <TEntity>)sortingMethod .MakeGenericMethod(typeof(TEntity), propertyType) .Invoke(null, new object[] { query, convertedExpression })); }