public ActionResult Index() { List<LaptopViewModel> laptopList = new List<LaptopViewModel>(); var laps = db.Laptops.ToList(); foreach (Laptop lap in laps) { LaptopViewModel lapView = new LaptopViewModel(); ApplicationUser _user = lap.ApplicationUser; lapView._laptop = lap; lapView._username = _user.FirstName + " " + _user.LastName; lapView._lastcheckout = lap.Checkouts .OrderBy(x => x.dtCheckedOut) .LastOrDefault() == null ? null : lap.Checkouts.OrderBy(x => x.dtCheckedOut) .Select(x => new CheckoutViewModel { dtCheckedOut = x.dtCheckedOut, dtReturned = x.dtReturned, Username = x.ApplicationUser.FirstName + " " + x.ApplicationUser.LastName }).LastOrDefault(); lapView._sold = lap.Sale ?? null; laptopList.Add(lapView); } return View(laptopList.AsEnumerable()); }
public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Laptop laptop = db.Laptops.Find(id); if (laptop == null) { return HttpNotFound(); } LaptopViewModel details = new LaptopViewModel(); details._laptop = laptop; details._lastcheckout = laptop.Checkouts.Any() ? laptop.Checkouts.OrderByDescending(x => x.dtCheckedOut).Select(x => new CheckoutViewModel { dtCheckedOut = x.dtCheckedOut, dtReturned = x.dtReturned, Username = x.ApplicationUser.FirstName + " " + x.ApplicationUser.LastName }).First() : null; details._sold = laptop.Sale == null ? null : laptop.Sale; return View(details); }