我有一个只包含一周训练(31 次锻炼)的时间表,现在我需要生成其余的锻炼,直到时间表结束(例如 20120601-20120831)。所以我想也许循环锻炼并将 addDays(7) 放在所有锻炼日期上。
我曾尝试使用 foreach 和 a 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;
}