예제 #1
0
 /// <summary>
 /// 获取实体集
 /// </summary>
 /// <param name="specification"></param>
 /// <param name="orderBy"></param>
 /// <param name="orderType"></param>
 /// <param name="navigationProperties"></param>
 /// <returns></returns>
 public virtual IList <TEntity> GetByCondition(ISpecification <TEntity> specification,
                                               Expression <Func <TEntity, dynamic> > orderBy,
                                               CoreEnum.SortOrder orderType
                                               , params Expression <Func <TEntity, dynamic> >[] navigationProperties)
 {
     return(this._repository.GetByCondition(specification, orderBy, orderType, navigationProperties));
 }
예제 #2
0
 /// <summary>
 /// 获取分页实体集
 /// </summary>
 /// <param name="specification"></param>
 /// <param name="orderBy"></param>
 /// <param name="orderType"></param>
 /// <param name="pageSize"></param>
 /// <param name="currentPage"></param>
 /// <param name="eagerLoadingProperties"></param>
 /// <returns></returns>
 public virtual CorePageResult <List <TEntity> > GetByCondition(
     ISpecification <TEntity> specification,
     Expression <Func <TEntity, dynamic> > orderBy,
     CoreEnum.SortOrder orderType,
     int pageSize, int currentPage
     , params Expression <Func <TEntity, dynamic> >[] navigationProperties)
 {
     return(this._repository.GetByCondition(specification, orderBy, orderType, pageSize, currentPage, navigationProperties));
 }
예제 #3
0
        /// <summary>
        /// 获取分页实体集
        /// </summary>
        /// <param name="specification"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderType"></param>
        /// <param name="pageSize"></param>
        /// <param name="currentPage"></param>
        /// <param name="eagerLoadingProperties"></param>
        /// <returns></returns>
        public virtual CorePageResult <List <TEntity> > GetByCondition(
            ISpecification <TEntity> specification,
            Expression <Func <TEntity, dynamic> > orderBy,
            CoreEnum.SortOrder orderType,
            int pageSize, int currentPage
            , params Expression <Func <TEntity, dynamic> >[] navigationProperties)
        {
            DbSet <TEntity>      dbSet     = this._dbContext.Set <TEntity>();
            IQueryable <TEntity> queryable = null;// this._dbContext.Set<TEntity>().Where(specification.GetExpression());

            if (navigationProperties != null &&
                navigationProperties.Length > 0)
            {
                var eagerLoadingProperty = navigationProperties[0];
                var eagerLoadingPath     = this.GetEagerLoadingPath(eagerLoadingProperty);
                var dbquery = dbSet.Include(eagerLoadingPath);
                for (int i = 1; i < navigationProperties.Length; i++)
                {
                    eagerLoadingProperty = navigationProperties[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 == CoreEnum.SortOrder.Asc)
            {
                queryableData = queryable.SortBy(orderBy);
            }
            else
            {
                queryableData = queryable.SortByDescending(orderBy);
            }
            queryableData = queryableData.Skip(pageSize * (currentPage - 1)) //跳过行数,最终生成的sql语句是Top(n)
                            .Take(pageSize).AsQueryable();                   //返回指定数量的行
            int nCount = queryable.Count();                                  //获取总记录数

            List <TEntity> col = queryableData.ToList();

            return(new CorePageResult <List <TEntity> >(nCount,
                                                        (nCount + pageSize - 1) / pageSize,
                                                        pageSize,
                                                        currentPage, col));
        }
예제 #4
0
        /// <summary>
        /// 排序方法
        /// </summary>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        private static string GetSortingMethodName(CoreEnum.SortOrder sortOrder)
        {
            switch (sortOrder)
            {
            case CoreEnum.SortOrder.Asc:
                return("OrderBy");

            case CoreEnum.SortOrder.Desc:
                return("OrderByDescending");

            default:
                throw new ArgumentException("Sort Order must be specified as either Ascending or Descending.",
                                            "sortOrder");
            }
        }
예제 #5
0
        /// <summary>
        /// 获取实体集
        /// </summary>
        /// <param name="specification"></param>
        /// <param name="orderBy"></param>
        /// <param name="orderType"></param>
        /// <param name="navigationProperties"></param>
        /// <returns></returns>
        public virtual IList <TEntity> GetByCondition(ISpecification <TEntity> specification,
                                                      Expression <Func <TEntity, dynamic> > orderBy,
                                                      CoreEnum.SortOrder orderType
                                                      , params Expression <Func <TEntity, dynamic> >[] navigationProperties)
        {
            DbSet <TEntity>      dbSet     = this._dbContext.Set <TEntity>();
            IQueryable <TEntity> queryable = null; //this._dbContext.Set<TEntity>().Where(specification.GetExpression());

            if (navigationProperties != null &&
                navigationProperties.Length > 0)
            {
                var eagerLoadingProperty = navigationProperties[0];
                var eagerLoadingPath     = this.GetEagerLoadingPath(eagerLoadingProperty);
                var dbquery = dbSet.Include(eagerLoadingPath);
                for (int i = 1; i < navigationProperties.Length; i++)
                {
                    eagerLoadingProperty = navigationProperties[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 == CoreEnum.SortOrder.Asc)
                {
                    queryable = queryable.SortBy(orderBy);
                }
                else
                {
                    queryable = queryable.SortByDescending(orderBy);
                }
            }
            List <TEntity> col = queryable.ToList();

            col.ToList().ForEach(t => { t.FormatInitValue(); });
            return(col);
        }
예제 #6
0
        /// <summary>
        /// 执行排序
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="query"></param>
        /// <param name="sortPredicate"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        private static IOrderedQueryable <TEntity> InvokeSortBy <TEntity>(IQueryable <TEntity> query,
                                                                          Expression <Func <TEntity, dynamic> > sortPredicate, CoreEnum.SortOrder sortOrder)
            where TEntity : class
        {
            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 }));
        }