Пример #1
0
        public async Task <List <Article> > GetAllArticles(int page, int pageSize)
        {
            // Try to clean Include() what happens?
            IIncludableQueryable <Article, ICollection <Comment> > queryableArticles = _context.Articles
                                                                                       .Where(a => a.PublishedOn < DateTime.Now
                                                                                              //&& a.VisibilityType == VisibilityType.PUBLIC)
                                                                                              ).Include(a => a.User)
                                                                                       .Include(a => a.Comments);

            var totalCount = await queryableArticles.CountAsync();

            List <Article> articles = await queryableArticles.Skip(page *pageSize)
                                      .Take(pageSize)
                                      .ToListAsync();

            return(articles);
        }
Пример #2
0
        public async Task <IActionResult> Pagination(int page = 1)
        {
            int pageSize = 1;
            IIncludableQueryable <MetanitExampleCoreMVC.Models.User, Company> source = dbUsers.Users.Include(x => x.Company);
            var count = await source.CountAsync();

            var items = await source.Skip((page - 1) *pageSize).Take(pageSize).ToListAsync();

            PageViewModel      pageViewModel = new PageViewModel(count, page, pageSize);
            IndexPageViewModel viewModel     = new IndexPageViewModel()
            {
                PageViewModel = pageViewModel,
                Users         = items
            };

            return(View(viewModel));
        }
Пример #3
0
        public static async Task <PagedResult <U> > ToPageResultAsync <T, V, U>(this IIncludableQueryable <T, V> query, int page, int pageSize, IConfigurationProvider config) where U : class
        {
            var result = new PagedResult <U>();

            result.CurrentPage = page;
            result.PageSize    = pageSize;
            result.RowCount    = await query.CountAsync();

            var pageCount = (double)result.RowCount / pageSize;

            result.PageCount = (int)Math.Ceiling(pageCount);

            var skip = (page - 1) * pageSize;

            result.Results = await query.Skip(skip)
                             .Take(pageSize)
                             .ProjectTo <U>(config)
                             .ToListAsync();

            return(result);
        }