public virtual IPagedList <TblProducts> GetBestSelling(int pageIndex = 1, int pageSize = int.MaxValue, int?filterByCategory = null, DateTime?fromDate = null) { IQueryable <TblInvoiceDetails> invoiceQuery; if (fromDate != null) { invoiceQuery = _dbContext.Invoices.Where(p => p.CreateDate > fromDate).SelectMany(p => p.InvoiceDetails) .Where(p => p.ItemType == InvoiceDetailsItemType.Product); } else { invoiceQuery = _dbContext.InvoiceDetails.Where(p => p.ItemType == InvoiceDetailsItemType.Product); } var sortedItems = invoiceQuery .GroupBy(p => p.ItemId) .Select(p => new { p.FirstOrDefault().ItemId, Sum = p.Sum(c => c.Qty) }).OrderByDescending(p => p.Sum) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .FromCache(CacheTags.Invoice).Select(p => p.ItemId).ToList(); var query = _dbContext.Products.Where(p => p.Published); if (filterByCategory != null) { var subCategories = _categoriesService.GetSubCategories(filterByCategory.Value); query = query.Where(p => p.Categories.Any(x => subCategories.Contains(x.Id))); } var products = query .Where(p => sortedItems.Contains(p.Id)) .Include(p => p.Descriptions) .Include(p => p.Images) .Include(p => p.Categories) .FromCache(_cacheKey, CacheTags.PostCategory, CacheTags.PostDescription, CacheTags.PostImage); var result = new StaticPagedList <TblProducts>( products.OrderBy(p => sortedItems.IndexOf(p.Id)), pageIndex, pageSize, invoiceQuery .GroupBy(p => p.ItemId) .Select(p => new { p.FirstOrDefault().ItemId, Sum = p.Sum(c => c.Qty) }).DeferredCount() .FromCache(CacheTags.Invoice)); return(result); }
public virtual IPagedList <T> GetNewItems(int pageIndex = 1, int pageSize = int.MaxValue, int?filterByCategory = null, DateTime?fromDate = null) { var query = _dbContext.Set <T>().Where(p => p.Published); if (fromDate != null) { query = _dbContext.Set <T>().Where(p => p.PublishDate >= fromDate); } if (filterByCategory != null) { var subCategories = _categoriesService.GetSubCategories(filterByCategory.Value); query = query.Where(p => p.Categories.Any(x => subCategories.Contains(x.Id))); } var result = new StaticPagedList <T>( query .OrderByDescending(p => p.PinToTop) .ThenByDescending(p => p.LastUpDate) .ThenByDescending(p => p.PublishDate) .Include(p => p.Categories) .Include(p => p.Descriptions) .Include(p => p.Images) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .FromCache(_cacheKey, CacheTags.PostCategory, CacheTags.PostDescription, CacheTags.PostImage), pageIndex, pageSize, query .DeferredCount() .FromCache(_cacheKey)); return(result); }