public async Task <IActionResult> AdvertisementsCategory(int?id, int page = 1, string type = "", int minPrice = 0, int maxPrice = 12000, IList <int> attributes = null) { int itemsPerPage = 12; int skip = itemsPerPage * (page - 1); int count = 0; IEnumerable <Advertisement> applicationDbContext = null; if (type == "") { count = await _context.Set <AdvertisementCategory>().Where(pc => pc.CategoryId == id).Select(pc => pc.Advertisement).CountAsync(); applicationDbContext = await _context.Set <AdvertisementCategory>().Where(pc => pc.CategoryId == id).Select(pc => pc.Advertisement).Include(p => p.Image).OrderByDescending(p => p.DateTime).Skip(skip).Take(itemsPerPage).ToListAsync(); } else if (attributes.Count() == 0) { count = await _context.Set <AdvertisementCategory>().Where(pc => pc.CategoryId == id).Select(pc => pc.Advertisement).Include(p => p.Image).Where(p => p.Price >= minPrice && p.Price <= maxPrice).CountAsync(); applicationDbContext = await _context.Set <AdvertisementCategory>().Where(pc => pc.CategoryId == id).Select(pc => pc.Advertisement).Include(p => p.Image).Where(p => p.Price >= minPrice && p.Price <= maxPrice).OrderByDescending(p => p.DateTime).Skip(skip).Take(itemsPerPage).ToListAsync(); } else { count = await( from ac in _context.AdvertisementsCategories join a in _context.Advertisements on ac.AdvertisementId equals a.Id into AdvertisementsCategoriesAdvertisements where ac.CategoryId == id from aca in AdvertisementsCategoriesAdvertisements.DefaultIfEmpty() join aa in _context.AdvertisementsAttributes on aca.Id equals aa.AdvertisementId into AdvertisementsAttributesAdvertisements where aca.Price >= minPrice && aca.Price <= maxPrice from aaa in AdvertisementsAttributesAdvertisements.DefaultIfEmpty() where attributes.Contains(aaa.AttributeId) select new Advertisement { Id = aca.Id, Title = aca.Title, Image = aca.Image, Price = aca.Price, Currency = aca.Currency, DateTime = aca.DateTime } ).Distinct().CountAsync(); applicationDbContext = await( from ac in _context.AdvertisementsCategories join a in _context.Advertisements on ac.AdvertisementId equals a.Id into AdvertisementsCategoriesAdvertisements where ac.CategoryId == id from aca in AdvertisementsCategoriesAdvertisements.DefaultIfEmpty() join aa in _context.AdvertisementsAttributes on aca.Id equals aa.AdvertisementId into AdvertisementsAttributesAdvertisements where aca.Price >= minPrice && aca.Price <= maxPrice from aaa in AdvertisementsAttributesAdvertisements.DefaultIfEmpty() where attributes.Contains(aaa.AttributeId) select new Advertisement { Id = aca.Id, Title = aca.Title, Image = aca.Image, Price = aca.Price, Currency = aca.Currency, DateTime = aca.DateTime } ).OrderByDescending(p => p.DateTime).Skip(skip).Take(itemsPerPage).Distinct().ToListAsync(); } decimal totalPages = count / itemsPerPage; int pages = (int)Math.Ceiling(totalPages); ViewData["id"] = id; ViewData["count"] = count; ViewData["page"] = page; ViewData["itemsPerPage"] = itemsPerPage; ViewData["pages"] = pages; ViewData["minPrice"] = minPrice; ViewData["maxPrice"] = maxPrice; ViewData["attributes"] = attributes; return(View("Advertisements/Category", applicationDbContext)); }
public IViewComponentResult Invoke(string type = "", int minPrice = 0, int maxPrice = 0, int id = 0) { IEnumerable <Attribute> attribute = null; if (id == 0) { if (type == "Advertisement") { attribute = ( from ad in _context.AdvertisementsAttributes join at in _context.Attributes on ad.AttributeId equals at.Id into aaa from a in aaa select new Attribute { Id = a.Id, Name = a.Name, Slug = a.Slug, Description = a.Description, Parent = a.Parent, ParentId = a.ParentId } ).GroupBy(x => x.Id).Select(x => x.First()).OrderBy(a => a.Name); } else { attribute = ( from ad in _context.ProductsAttributes join at in _context.Attributes on ad.AttributeId equals at.Id into aaa from a in aaa select new Attribute { Id = a.Id, Name = a.Name, Slug = a.Slug, Description = a.Description, Parent = a.Parent, ParentId = a.ParentId } ).GroupBy(x => x.Id).Select(x => x.First()).OrderBy(a => a.Name); } } else { if (type == "Advertisement") { attribute = ( from ac in _context.AdvertisementsCategories join a in _context.Advertisements on ac.AdvertisementId equals a.Id into AdvertisementsCategoriesAdvertisements where ac.CategoryId == id from aca in AdvertisementsCategoriesAdvertisements.DefaultIfEmpty() join aa in _context.AdvertisementsAttributes on aca.Id equals aa.AdvertisementId into AdvertisementsAttributesAdvertisements from aaa in AdvertisementsAttributesAdvertisements.DefaultIfEmpty() join a in _context.Attributes on aaa.AttributeId equals a.Id into AttributesList from al in AttributesList.DefaultIfEmpty() select new Attribute { Id = al.Id, Name = al.Name, Slug = al.Slug, Description = al.Description, Parent = al.Parent, ParentId = al.ParentId } ).GroupBy(x => x.Id).Select(x => x.First()).OrderBy(a => a.Name); } else { attribute = ( from pc in _context.ProductsCatalogs join p in _context.Products on pc.ProductId equals p.Id into CatalogProducts where pc.CatalogId == id from cp in CatalogProducts join pa in _context.ProductsAttributes on cp.Id equals pa.ProductId into PAttribures from psa in PAttribures join at in _context.Attributes on psa.AttributeId equals at.Id into aaa from a in aaa select new Attribute { Id = a.Id, Name = a.Name, Slug = a.Slug, Description = a.Description, Parent = a.Parent, ParentId = a.ParentId } ).GroupBy(x => x.Id).Select(x => x.First()).ToList(); } } ViewData["type"] = type; ViewData["minPrice"] = minPrice; ViewData["maxPrice"] = maxPrice; ViewData["id"] = id; return(View("Default", attribute)); }