如何在计划中生成缺少的日期?

时间:2012-08-05 21:44:14

标签: c#

我的计划只包含一周的训练(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;
    }

1 个答案:

答案 0 :(得分:0)

DateTimeimmutable。也就是说,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列表中的项目相同。主要区别在于,在我的WorkoutInfo中,您不会保留锻炼发生的特定时间点,而是保持从一周开始的抵消TimeSpan成员WeekOffset的一种形式。通过这种表示,将一周的计划延长到整个计划表会更加直截了当,如您所见。

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

如果您遇到任何困难,请告诉我。