Пример #1
0
 /// <summary>
 /// 获取分页列表
 /// </summary>
 /// <param name="pagetion"></param>
 /// <returns></returns>
 public virtual IQueryable <TEntity> GetList(Pagetion pagetion)
 {
     if (pagetion == null)
     {
         throw new ArgumentNullException();
     }
     return(GetList(pagetion, false));
 }
Пример #2
0
        /// <summary>
        /// 获取筛选结果后的分页列表,按照CreateTime asc排序
        /// </summary>
        /// <param name="whereLamb">过滤表达式</param>
        /// <param name="pagetion"></param>
        /// <param name="orderByAscCreateTime"></param>
        /// <returns></returns>
        public virtual IQueryable <TEntity> GetList(Expression <Func <TEntity, bool> > whereLamb, Pagetion pagetion, bool orderByAscCreateTime)
        {
            if (pagetion == null)
            {
                throw new ArgumentNullException("参数不能为空!");
            }
            Type typeEntity = typeof(TEntity);

            IQueryable <TEntity> entities = null;

            if (whereLamb == null)
            {
                entities = GetList();
            }
            else
            {
                entities = GetList(whereLamb);
            }

            #region 动态构建orderby
            string strOrderby = orderByAscCreateTime ? "OrderBy" : "orderbydescending";
            //创建一个参数c
            ParameterExpression param =
                Expression.Parameter(typeof(TEntity), "t");

            MethodCallExpression orderByCallExpression = null;

            if (typeEntity.GetProperty(BaseFields.CreateTime) != null)
            {
                orderByCallExpression = Expression.Call(
                    typeof(Queryable), strOrderby,
                    new Type[] { typeof(TEntity), typeof(DateTime) },
                    Expression.Constant(entities),
                    Expression.Lambda(Expression.Property
                                          (param, BaseFields.CreateTime), param));
            }
            else if (typeEntity.GetProperty(BaseFields.Sort) != null)
            {
                orderByCallExpression = Expression.Call(
                    typeof(Queryable), "OrderBy",
                    new Type[] { typeof(TEntity), typeof(int) },
                    Expression.Constant(entities),
                    Expression.Lambda(Expression.Property
                                          (param, BaseFields.Sort), param));
            }
            if (orderByCallExpression == null)
            {
                throw new Exception("目标表至少要有CreateTime 获取Sort作为排序字段");
            }
            #endregion

            //生成动态查询
            IQueryable <TEntity> query = entities
                                         .Provider.CreateQuery <TEntity>(orderByCallExpression).Skip(pagetion.rows * (pagetion.page - 1)).Take(pagetion.rows);
            pagetion.total = entities.Count();
            return(query);
        }
Пример #3
0
 /// <summary>
 /// 获取分页列表,按照CreateTime asc排序
 /// </summary>
 /// <param name="pagetion"></param>
 /// <param name="orderbyAscCreateTime">按照CreateTime 顺序排列</param>
 /// <returns></returns>
 public virtual IQueryable <TEntity> GetList(Pagetion pagetion, bool orderbyAscCreateTime)
 {
     return(GetList(null, pagetion, orderbyAscCreateTime));
 }