Example #1
0
        public MyQueryable <T> OrderBy <TProperty>(Expression <Func <T, TProperty> > expression,
                                                   MyDbOrderBy orderBy = MyDbOrderBy.Asc)
        {
            if (expression.Body.NodeType == ExpressionType.MemberAccess)
            {
                _orderBy = GetOrderByString((MemberExpression)expression.Body);
                if (orderBy == MyDbOrderBy.Desc)
                {
                    _orderBy += " DESC";
                }
            }

            return(this);
        }
Example #2
0
        public async Task <List <T> > FetchAsync <T>(Expression <Func <T, bool> > where = null, Expression <Func <T, object> > orderBy = null,
                                                     MyDbOrderBy dbSort = MyDbOrderBy.Asc) where T : class, new()
        {
            var query = new MyQueryable <T>(_connectionString);

            if (where != null)
            {
                query.Where(where);
            }

            if (orderBy != null)
            {
                query.OrderBy(orderBy, dbSort);
            }

            return(await query.ToListAsync());
        }
Example #3
0
        public MyQueryable <T> ThenOrderBy <TProperty>(Expression <Func <T, TProperty> > expression,
                                                       MyDbOrderBy orderBy = MyDbOrderBy.Asc)
        {
            if (string.IsNullOrWhiteSpace(_orderBy))
            {
                throw new ArgumentNullException(nameof(_orderBy), "排序字段为空,必须先调用OrderBy或OrderByDesc才能调用此方法");
            }
            if (expression.Body.NodeType == ExpressionType.MemberAccess)
            {
                _orderBy += "," + GetOrderByString((MemberExpression)expression.Body);
                if (orderBy == MyDbOrderBy.Desc)
                {
                    _orderBy += " DESC";
                }
            }

            return(this);
        }
Example #4
0
        /// <summary>
        /// 根据条件加载一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where">查询条件</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="dbSort">正序或倒序</param>
        /// <returns>实体,若记录为空,返回default(T)</returns>
        public T Load <T>(Expression <Func <T, bool> > where     = null,
                          Expression <Func <T, object> > orderBy = null,
                          MyDbOrderBy dbSort = MyDbOrderBy.Asc) where T : class, new()
        {
            var query = new MyQueryable <T>(_connectionString);

            if (where != null)
            {
                query.Where(where);
            }

            if (orderBy != null)
            {
                query.OrderBy(orderBy, dbSort);
            }

            return(query.FirstOrDefault());
        }
Example #5
0
        public async Task <PagingResult <T> > PageListAsync <T>(int pageIndex,
                                                                int pageSize,
                                                                Expression <Func <T, bool> > where     = null,
                                                                Expression <Func <T, object> > orderBy = null,
                                                                MyDbOrderBy dbSort = MyDbOrderBy.Asc) where T : class, new()
        {
            var query = new MyQueryable <T>(_connectionString);

            if (where != null)
            {
                query.Where(where);
            }

            if (orderBy != null)
            {
                query.OrderBy(orderBy, dbSort);
            }

            return(await query.ToPageListAsync(pageIndex, pageSize));
        }
Example #6
0
        /// <summary>
        /// 加载分页列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="recordCount">记录总数</param>
        /// <param name="where">查询条件</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="dbSort">正序或倒序</param>
        /// <returns>实体列表,输出记录总数</returns>
        public List <T> PageList <T>(int pageIndex,
                                     int pageSize,
                                     out int recordCount,
                                     Expression <Func <T, bool> > where     = null,
                                     Expression <Func <T, object> > orderBy = null,
                                     MyDbOrderBy dbSort = MyDbOrderBy.Asc) where T : class, new()
        {
            var query = new MyQueryable <T>(_connectionString);

            if (where != null)
            {
                query.Where(where);
            }

            if (orderBy != null)
            {
                query.OrderBy(orderBy, dbSort);
            }

            return(query.ToPageList(pageIndex, pageSize, out recordCount));
        }