public async Task <FadActionResult <T> > QueryAsync(GridQueryModel args = null, IList <string> fields = null)
        {
            var type = System.Reflection.Assembly.GetExecutingAssembly().GetType(nameof(args.Entity));

            var actionResult = new FadActionResult <T>();

            var query = _context.Set <T>().AsQueryable();

            //filter
            if (args != null && args.Filtered != null && args.Filtered.Length > 0)
            {
                var filterExpression = GridQueryUtility.FilterExpression <T>(args.Filtered[0].id, args.Filtered[0].value);
                for (int i = 1; i < args.Filtered.Length; i++)
                {
                    filterExpression = GridQueryUtility.FilterExpression <T>(args.Filtered[i].id, args.Filtered[i].value);
                }
                query = query.Where(filterExpression);
            }

            //total count
            var total = await query.CountAsync();

            //sort
            if (args != null && args.Sorted != null && args.Sorted.Length > 0)
            {
                for (int i = 0; i < args.Sorted.Length; i++)
                {
                    query = query.SortBy(args.Sorted[i].id, args.Sorted[i].desc);
                }
            }


            //projection
            if (fields != null)
            {
                query = query.SelectDynamic(fields);
            }

            var result = await query.Skip(args.Page *args.PageSize)
                         .Take(args.PageSize)
                         .ToListAsync();

            actionResult.Data        = result;
            actionResult.TotalCount  = Convert.ToInt32(Math.Ceiling(total / (float)args.PageSize));;
            actionResult.CurrentPage = args.Page;

            return(actionResult);
        }
示例#2
0
        public async Task <(IReadOnlyList <T> List, int Count)> GetAsync(
            Expression <Func <T, bool> > filter = null,
            Func <IQueryable <T>, IOrderedQueryable <T> > orderBy = null,
            string includeProperties = "", int page = 1, int pageItemCount = 15)
        {
            IQueryable <T> query = _dbContext.Set <T>();

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

                if (!string.IsNullOrEmpty(includeProperties))
                {
                    foreach (var includeProperty in includeProperties.Split
                                 (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        query = query.Include(includeProperty);
                    }
                }

                if (orderBy != null)
                {
                    query = orderBy(query);
                }

                int count = query.Count();

                if (page == 0)
                {
                    return(await query.ToListAsync(), count);
                }
                else
                {
                    query = query.Skip((page - 1) * pageItemCount).Take(pageItemCount);

                    var result = await query.ToListAsync();

                    return(result, count);
                }
            }
            catch (Exception ex)
            {
                //LogError(MethodBase.GetCurrentMethod().Name, ex);
                throw ex;
            }
        }