Пример #1
0
        public ActionResult Index()
        {
            var listView = new List<VehiclesViewModel>();
            var listVehicles = new List<Vehicle>();

            // Convert joined db graph to a typed Vehicle list
            var dbVehicles = (from v in db.Vehicles
                              join m in db.Makes on v.Make.ID equals m.ID
                              select new { m.Name, v.MPG }).ToList();

            foreach (var v in dbVehicles)
            {
                var vehicle = new Vehicle()
                {
                    Make = new Make() { Name = v.Name },
                    MPG = v.MPG
                };

                listVehicles.Add(vehicle);
            }

            var makes = db.Makes.ToList();
            var masterList = new List<List<Vehicle>>();

            // Arrange Vehicles by Make
            foreach (var m in makes)
            {
                var newList = new List<Vehicle>();

                foreach (var v in listVehicles)
                {
                    if (v.Make.Name == m.Name)
                        newList.Add(v);
                }

                masterList.Add(newList);
            }

            // Perform necessary operations and bind to the ViewModel
            foreach (var vehicleList in masterList)
            {
                if (vehicleList.Count != 0)
                {
                    var vm = new VehiclesViewModel()
                    {
                        Make = vehicleList.Select(c => c.Make.Name).FirstOrDefault(),
                        AvgMPG = (from v in vehicleList select v.MPG).Average(),
                        MaxMPG = (from v in vehicleList select v.MPG).Max(),
                        MinMPG = (from v in vehicleList select v.MPG).Min()
                    };

                    listView.Add(vm);
                }
            }

            return View(listView);
        }
Пример #2
0
        public ActionResult Add(HomeViewModel model)
        {
            if (model == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var userGraph = db.Users.Where(u => u.Email == User.Identity.Name).FirstOrDefault();
            if (userGraph != null)
            {
                var vehicleMake = db.Makes.Where(v => v.ID == model.SelectedVehicle).FirstOrDefault();
                var newVehicle = new Vehicle()
                {
                    Make = vehicleMake,
                    MPG = model.SelectedVehicleMPG
                };

                userGraph.Vehicles.Add(newVehicle);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }