private static Product GoodsToProducts(GoodInfoViewModel goodInfoViewModel, StationType[] stations) { var soldBy = goodInfoViewModel.SoldBy.Select(goods => stations.First(station => goods.StartsWith(station.CommonName))).ToArray(); var boughtBy = goodInfoViewModel.BoughtBy.Select(goods => stations.First(station => goods.StartsWith(station.CommonName))).ToArray(); return(new Product(goodInfoViewModel.Name, goodInfoViewModel.Volume, goodInfoViewModel.AvgPrice, goodInfoViewModel.Illegal, goodInfoViewModel.Dangerous, soldBy, boughtBy)); }
// GET: GoodInfoes public async Task <ActionResult> Index() { if (Session["CurrentUserID"] == null) { RedirectToAction("Login", "Home"); } int CurrentUserID = (int)Session["CurrentUserID"]; //return View(await db.GoodInfoes.ToListAsync()); List <GoodInfoViewModel> VMGoodInfo = new List <GoodInfoViewModel>(); var AllGoods = await db.GoodInfoes.Where(d => d.UserID == CurrentUserID).ToListAsync(); foreach (GoodInfo good in AllGoods) { double averagecost = 0; var storage = db.Storages.Where(d => d.GoodID == good.Id && d.UserID == CurrentUserID); if (storage.Any()) { averagecost = storage.Average(d => d.Cost); } double averageprice = 0; int totalsold = 0; var goodordered = db.OrderedGoods.Where(d => d.GoodID == good.Id && d.UserID == CurrentUserID); if (goodordered.Any()) { averageprice = goodordered.Average(d => d.Price); totalsold = goodordered.Sum(d => d.Quantity); } GoodInfoViewModel vm = new GoodInfoViewModel { GoodInfo = good, AverageCost = averagecost.ToString("F"), AveragePrice = averageprice.ToString("F"), TotalSold = totalsold }; VMGoodInfo.Add(vm); } return(View(VMGoodInfo)); }