1週間のトレーニング(31回のトレーニング)のみを含むスケジュールがあり、スケジュールが終了するまで残りのトレーニングを生成する必要があります(例:20120601-20120831)。だから私は多分ワークアウトをループしてすべてのworkout.dateにaddDays(7)を置くことを考えました。
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;
}