// GET: NextRaces public ActionResult Index() { var distances = db.distances; var list = distances.ToList(); ViewBag.distances = buildSelectList(distances); // var allRunners = db.runners.Include(n => n.LastRaces).OrderBy(n =>n.secondname); var allRunners = db.runners.OrderBy(n => n.secondname); List <nextRaceVM> vm = new List <nextRaceVM>(); foreach (var m in allRunners) { var nr = new nextRaceVM(); var last = db.LastRaces.SingleOrDefault(l => l.RunnerId == m.EFKey); if (last != null) { nr.RunnerId = m.EFKey; nr.LastDistance = db.distances.SingleOrDefault(d => d.Value == last.Distance).Name; nr.LastTime = RaceCalc.formatTime(last.Time); nr.RunnerName = m.firstname + " " + m.secondname; nr.Time = RaceCalc.formatTime(last.Time); vm.Add(nr); } } return(View(vm)); }
public ActionResult NewTarget(string distances) { db.NextRaces.RemoveRange(db.NextRaces); var allRunners = db.runners.Include(n => n.LastRaces); // foreach runner foreach (var r in allRunners) { var lastRace = r.LastRaces.FirstOrDefault(s => s.RunnerId == r.EFKey); if (lastRace != null) { var oldDistance = lastRace.Distance; var oldtime = lastRace.Time; var predictedTime = (RaceCalc.calcPredictedTime(oldDistance, Convert.ToDouble(distances), oldtime) + RaceCalc.cameron(oldDistance, Convert.ToDouble(distances), oldtime)) / 2; RunningModel.NextRace nextrace = new NextRace(); nextrace.RunnerId = r.EFKey; nextrace.Distance = Convert.ToDouble(distances); nextrace.Time = Convert.ToInt32(predictedTime); nextrace.Active = true; db.NextRaces.Add(nextrace); } } db.SaveChanges(); // get last race time and distance // calculate next time for next time // update next date time and distance // contruct view model to display new times. var allNextRaces = db.NextRaces; List <nextRaceVM> vm = new List <nextRaceVM>(); foreach (var m in allNextRaces) { var nr = new nextRaceVM(); var last = db.LastRaces.SingleOrDefault(l => l.RunnerId == m.RunnerId); nr.LastDistance = db.distances.SingleOrDefault(d => d.Value == last.Distance).Name; nr.LastTime = RaceCalc.formatTime(last.Time); nr.RunnerName = m.runner.firstname + " " + m.runner.secondname; nr.Time = RaceCalc.formatTime(m.Time); vm.Add(nr); } var ds = Convert.ToDouble(distances); ViewBag.NewDistance = db.distances.SingleOrDefault(d => d.Value == ds).Name; return(View(vm)); }