Пример #1
0
        public async Task <LinkPagedVM> GetLinksAsync(int page, int pageSize, int categoryId, string keywords)
        {
            var vm = new LinkPagedVM
            {
                PageIndex  = page - 1,
                PageSize   = pageSize,
                Keywords   = keywords,
                CategoryId = categoryId
            };

            var query = _context.Links.Include(d => d.Category).AsQueryable();

            if (vm.CategoryId > 0)
            {
                query = query.Where(d => d.CategoryId == vm.CategoryId);
            }
            if (!string.IsNullOrEmpty(keywords))
            {
                query = query.Where(d => d.Title.Contains(keywords));
            }

            vm.RowCount = await query.CountAsync();

            vm.ItemList = await query.OrderByDescending(d => d.Importance).ThenByDescending(d => d.CreatedDate)
                          .Skip(vm.PageIndex * vm.PageSize).Take(vm.PageSize).ProjectTo <LinkVM>(_mapper.ConfigurationProvider).ToListAsync();

            return(vm);
        }
Пример #2
0
        public async Task <LinkPagedVM> GetLinksAsync(int page,
                                                      int pageSize,
                                                      string keywords,
                                                      string orderBy,
                                                      string orderMode,
                                                      int categoryId = 0)
        {
            var vm = new LinkPagedVM
            {
                PageIndex  = page - 1,
                PageSize   = pageSize,
                Keywords   = keywords,
                OrderBy    = orderBy,
                OrderMode  = orderMode,
                CategoryId = categoryId
            };

            var query = _db.Links.AsNoTracking().Include(d => d.Category).AsQueryable();

            if (!string.IsNullOrEmpty(keywords))
            {
                query = query.Where(d => d.Title.Contains(keywords) || d.Description.Contains(keywords));
            }

            if (categoryId > 0)
            {
                query = query.Where(d => d.CategoryId == categoryId);
            }

            if (!string.IsNullOrEmpty(orderBy))
            {
                switch (orderBy)
                {
                case "title":
                    if (orderMode == "desc")
                    {
                        query = query.OrderByDescending(d => d.Title);
                    }
                    else
                    {
                        query = query.OrderBy(d => d.Title);
                    }
                    break;

                case "createdDate":
                    if (orderMode == "desc")
                    {
                        query = query.OrderByDescending(d => d.CreatedDate);
                    }
                    else
                    {
                        query = query.OrderBy(d => d.CreatedDate);
                    }
                    break;

                default:
                    query = query.OrderByDescending(d => d.CreatedDate);
                    break;
                }
            }
            else
            {
                query = query.OrderByDescending(d => d.CreatedDate);
            }



            vm.RowCount = await query.CountAsync();

            vm.ItemList = await query.Skip(vm.PageIndex *vm.PageSize).Take(vm.PageSize).ProjectTo <LinkVM>(_mapper.ConfigurationProvider).ToListAsync();

            return(vm);
        }