/// <summary> /// Prepare Latest And Feature Products /// </summary> /// <param name="products"></param> /// <returns></returns> private LatestAndFeatureProductsViewModel PrepareLatestAndFeatureProducts(IEnumerable <Product> products) { try { LatestAndFeatureProductsViewModel lpViewModel = new LatestAndFeatureProductsViewModel(); var latestProds = products.OrderBy(p => p.DisplayOrder).ToList(); var featureProductsList = new List <FeatureProductsViewModel>(); var latestProductsList = new List <LatestProductsViewModel>(); for (int i = 0; i < latestProds.Count; i++) { if (i < 20) { FeatureProductsViewModel featureProductsViewModel = new FeatureProductsViewModel { ProductName = latestProds[i].ProductName, Description = latestProds[i].Description, ShortDescription = latestProds[i].ShortDescription, DisplayOrder = latestProds[i].DisplayOrder, CreatedDate = latestProds[i].CreatedDate, UpdatedDate = latestProds[i].UpdatedDate, IsActive = latestProds[i].IsActive, Price = latestProds[i].Price, Quantity = latestProds[i].Quantity, SubCategoryOneId = latestProds[i].SubCategoryOneId }; featureProductsList.Add(featureProductsViewModel); LatestProductsViewModel latestProductsViewModel = new LatestProductsViewModel { ProductName = latestProds[i].ProductName, Description = latestProds[i].Description, ShortDescription = latestProds[i].ShortDescription, DisplayOrder = latestProds[i].DisplayOrder, CreatedDate = latestProds[i].CreatedDate, UpdatedDate = latestProds[i].UpdatedDate, IsActive = latestProds[i].IsActive, Price = latestProds[i].Price, Quantity = latestProds[i].Quantity, SubCategoryOneId = latestProds[i].SubCategoryOneId }; latestProductsList.Add(latestProductsViewModel); } else { break; } } lpViewModel.FeatureProductsVM = new List <FeatureProductsViewModel>(); lpViewModel.FeatureProductsVM = featureProductsList.OrderBy(d => d.DisplayOrder); lpViewModel.LatestProductsVM = new List <LatestProductsViewModel>(); lpViewModel.LatestProductsVM = latestProductsList.OrderByDescending(l => l.CreatedDate); return(lpViewModel); } catch (Exception e) { throw e; } }
public ActionResult Index() { var latestProductsInDb = _context.Products.OrderByDescending(p => p.Id).Take(4).ToList(); var products = _context.Products.ToList(); List <Product> latestFeaturedProductsInDb = new List <Product>(); List <Product> topRatedProductsInDb = new List <Product>(); var maxRateOfPro = products.OrderByDescending(p => p.TotalPercentageRate).Take(4); foreach (var pro in maxRateOfPro) { topRatedProductsInDb.Add(pro); } var viewModel = new LatestProductsViewModel() { latestProducts = latestProductsInDb, topRatedProducts = topRatedProductsInDb, }; return(View(viewModel)); }