예제 #1
0
        /// <summary>
        /// 根据自定义条件查询数据
        /// </summary>
        /// <typeparam name="PEntity"></typeparam>
        /// <param name="specification"></param>
        /// <param name="sortBy"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="desc"></param>
        /// <returns></returns>
        public List <T> GetPaged(ISpecification <T> specification, int pageIndex, int pageSize, out int totalCount, Func <T, object> sortBy, bool desc = true)
        {
            List <T> list = null;

            totalCount = 0;
            if (specification != null)
            {
                try
                {
                    using (BaseViewDbContext <T> db = new BaseViewDbContext <T>())
                    {
                        if (desc)
                        {
                            list = db.BaseDbSet.Where(specification.Predicate).OrderByDescending(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            list = db.BaseDbSet.Where(specification.Predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        totalCount = db.BaseDbSet.Where(specification.Predicate).Count();
                    }
                }
                catch (Exception ex)
                {
                    log.Error("获取失败", ex);
                    //throw new Exception("获取失败!", ex);
                }
            }
            return(list);
        }
예제 #2
0
        /// <summary>
        /// 获取该实体的查询
        /// </summary>
        /// <param name="top">前几条,默认排序</param>
        /// <param name="predicate">表达式</param>
        /// <param name="sortBy">排序字段</param>
        /// <param name="desc">默认True为降序,False升序</param>
        /// <returns></returns>
        public List <T> GetQuery(int top, Expression <Func <T, bool> > predicate, Func <T, object> sortBy, bool desc = true)
        {
            List <T> list = null;

            if (predicate != null)
            {
                try
                {
                    using (BaseViewDbContext <T> db = new BaseViewDbContext <T>())
                    {
                        if (desc)
                        {
                            list = db.BaseDbSet.Where(predicate).OrderByDescending(sortBy).Take(top).ToList();
                        }
                        else
                        {
                            list = db.BaseDbSet.Where(predicate).OrderBy(sortBy).Take(top).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error("获取失败", ex);
                    //throw new Exception("获取失败!", ex);
                }
            }
            return(list);
        }
예제 #3
0
        /// <summary>
        /// 获取该实体的查询
        /// </summary>
        /// <param name="top">前几条,默认排序</param>
        /// <param name="predicate">表达式</param>
        /// <returns></returns>
        public List <T> GetQuery(int top, Expression <Func <T, bool> > predicate)
        {
            List <T> list = null;

            if (predicate != null)
            {
                try
                {
                    using (BaseViewDbContext <T> db = new BaseViewDbContext <T>())
                    {
                        list = db.BaseDbSet.Where(predicate).Take(top).ToList();
                    }
                }
                catch (Exception ex)
                {
                    log.Error("获取失败", ex);
                    //throw new Exception("获取失败!", ex);
                }
            }
            return(list);
        }
예제 #4
0
        /// <summary>
        /// 根据lamda表达式获取单一实体,如果没有找到则返回null
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Single(Expression <Func <T, bool> > predicate)
        {
            T entity = new T();

            if (predicate != null)
            {
                try
                {
                    using (BaseViewDbContext <T> db = new BaseViewDbContext <T>())
                    {
                        entity = db.BaseDbSet.FirstOrDefault(predicate);
                    }
                }
                catch (Exception ex)
                {
                    log.Error("获取失败", ex);
                    //throw new Exception("获取失败!", ex);
                }
            }
            return(entity);
        }