2012-08-05 61 views
0

我有一個只包含一週訓練(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; 
    } 
+0

你只是將你的鍛鍊保持在列表中? (基本上是線性數組?)你可以將它們存儲在HashMap中嗎?然後,您可以使用DateTime的自定義子類作爲關鍵字,並從工作日開始查找鍛鍊,並開始計時 – olagjo 2012-08-05 22:20:17

回答

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類合併,這取決於你的項目的全貌。

如果您遇到更多困難,請告訴我。

+0

問題是每天有超過一次的鍛鍊。 – n3tx 2012-08-05 21:53:14

+0

因此,如果我做得對,在您的日程安排中您定義了一週的鍛鍊,並且您希望將此模式延伸到整個計劃。如果這是正確的,我認爲你不是很好地模擬這種情況。該功能並沒有給出它應該達到什麼的清晰印象。 – twoflower 2012-08-05 22:04:14

+0

是的,這是正確的。我很抱歉造型,但不知道我在做什麼。 – n3tx 2012-08-05 22:06:59