我有一個只包含一週訓練(31次訓練)的時間表,現在我需要生成其餘的訓練,直到時間表結束(例如20120601-20120831)。 所以我想可能循環鍛鍊,並把addDays(7)放在所有workout.date上。如何在計劃中生成缺失的日期?
我已經嘗試使用foreach和for,但不能得到它的工作。
我GenerateSchedule方法:
public ActionResult GenerateSchedule(int scheduleId)
{
//get schedule included list of 31 workouts
//only added one week on workouts
var schedule = _scheduleServices.GetSchedule(scheduleId);
var workouts = schedule.Workouts.ToList();
int i = 0;
for(var day = schedule.FromDate; day.Date <= schedule.ToDate; day = day.AddDays(1))
{
foreach (var workout in workouts)
{
}
var newWorkout = new WorkoutFormModel
{
ScheduleId = workouts[i].ScheduleId,
Date = workouts[i].Date.AddDays(7),
StartTime = workouts[i].StartTime,
EndTime = workouts[i].EndTime,
InstructorId = workouts[i].Instructor.Id,
CourseId = workouts[i].Course.Id,
};
i++;
_workoutServices.AddWorkout(newWorkout);
}
/*
foreach(var workout in schedule.Workouts)
{
if (workout.Date.AddDays(7) <= schedule.ToDate)
{
var newWorkout = new WorkoutFormModel
{
ScheduleId = workout.ScheduleId,
Date = workout.Date.AddDays(7),
StartTime = workout.StartTime,
EndTime = workout.EndTime,
InstructorId = workout.Instructor.Id,
CourseId = workout.Course.Id,
};
_workoutServices.AddWorkout(newWorkout);
}
}*/
return RedirectToAction("Overview", new { scheduleId });
}
更新:
private static List<WorkoutFormModel> ExtendSchedule(Schedule schedule)
{
var workoutList = new List<WorkoutFormModel>();
for (var workoutStart = schedule.FromDate; workoutStart <= schedule.ToDate; workoutStart = workoutStart.AddDays(7))
{
workoutList.AddRange(schedule.Workouts.Select(Workout => new WorkoutFormModel
{
ScheduleId = schedule.Id,
Date = workoutStart.Add(Workout.WeekOffset),
StartTime = Workout.StartTime,
EndTime = Workout.EndTime,
Course = Workout.Course
}));
}
return workoutList;
}
你只是將你的鍛鍊保持在列表中? (基本上是線性數組?)你可以將它們存儲在HashMap中嗎?然後,您可以使用DateTime的自定義子類作爲關鍵字,並從工作日開始查找鍛鍊,並開始計時 – olagjo 2012-08-05 22:20:17