0

我有一个只包含一周训练(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;
    }
4

1 回答 1

0

DateTime不可变的。也就是说,AddDays()返回 的新实例DateTime,而您调用它的实例保持不变。所以你的循环可能看起来像这样:

for(var day = schedule.FromDate; day.Date <= schedule.ToDate; day = day.AddDays(1))

编辑:既然我们同意这更像是一个设计问题,我建议这样做:

  • 编写以下辅助函数,尽可能明确地说明其输入:

    private List<WorkoutFormModel> ExtendSchedule(Schedule schedule, IEnumerable<WorkoutInfo> oneWeekWorkouts)
    {
        var workoutList = new List<WorkoutFormModel>();
        foreach (var workoutStart = schedule.FromDate; workoutStart <= schedule.ToDate; workoutStart = workoutStart.AddDays(7))
        {
            workoutList.AddRange(oneWeekWorkouts.Select(workout => new WorkoutFormModel
                                 {
                                     ScheduleId = schedule.Id,
                                     Date = workoutStart.Add(workout.WeekOffset),
                                     StartTime = ?,
                                     EndTime = ?,
                                     InstructorId = workout.Instructor.Id,
                                     CourseId = workout.Course.Id
                                 }));
        }
    
        return workoutList;
    }
    

    WorkoutInfo代表锻炼的类在哪里。我将它命名为不同的名称,以便它不会与您使用的类型发生冲突,但与列表中的项目基本相同Schedule.Workouts。主要区别在于,在 my 中WorkoutInfo,您不会保留将要进行锻炼的特定时间点,而是以会员的形式从一周开始的偏移量。如您所见,使用这种表示形式,将一周的计划扩展到整个日程安排要简单得多。TimeSpanWeekOffset

由您来填补空白并将我的提议与您的代码库保持一致。也许你会想要创建这个新WorkoutInfo类,也许最好将它与你现有的Workout类合并,这取决于你项目的整体情况。

如果您遇到更多困难,请告诉我。

于 2012-08-05T21:47:50.803 回答