2012-08-10 67 views
0

我想改变日期到列表中的所有锻炼,其他属性工作正常,只有当我更改日期我得到问题。我尝试了不同的故障排除,但都没有奏效。我尝试使用updatedWorkout.Date作为workoutStart =超出范围。如果我使用old.Date,那么,如何在7天内添加新日期?更改日期到列表中的所有对象,并更新

也许有更好的方法来做到这一点?

这里是我的方法:

private int UpdateForAllWorkouts(IWorkoutCommand updatedWorkout) 
    { 
     try 
     { // get schedule 
      var schedule = _scheduleRepository.GetIncludeWorkouts(updatedWorkout.ScheduleId); 
      // get workouts within the schedule by schedule id 
      var workout = schedule.Workouts.FirstOrDefault(w => w.Id == updatedWorkout.Id); 

      for (var workoutStart = workout.Date; workoutStart <= schedule.ToDate; workoutStart = workoutStart.AddDays(7)) 
      { 
       // get specdfic workout by workout id and start time 
       var old = schedule.Workouts.Single(w => w.Date == workoutStart && w.StartTime == workout.StartTime); 

       var tmp = new Model.Workout 
           { 
            Id = old.Id, 
            CourseId = updatedWorkout.CourseId, 
            InstructorId = updatedWorkout.InstructorId, 
            Date = //<---problem 
            StartTime = updatedWorkout.StartTime, 
            EndTime = updatedWorkout.EndTime, 
            ScheduleId = updatedWorkout.ScheduleId, 
            WeekOffSet = updatedWorkout.WeekOffSet.Days 
           }; 
      } 

      return updatedWorkout.Id; 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(""); 
     } 
    } 

THX的帮助!

+0

设置“日期”字段时会出现什么问题? – horgh 2012-08-10 01:08:18

+0

有问题的存储库是什么?它是否了解身体锻炼与Id = old.Id应更新提供的值?因为我看到你创建新的锻炼,而不是更新旧的......这是EntityFramework或L2S(没有在标签中指定)? – horgh 2012-08-10 01:11:32

+0

当我通过workoutstart = updatedWorkout.Date设置日期时,我得到一个输出或范围。 – n3tx 2012-08-10 07:02:55

回答

0

我想你可以使用一个循环为7

这样的:

var workoutStart = workout.Date; 
while(true){ 

    if(workoutStart <= schedule.ToDate){ 
    // Do something 
    }else{ 
     break; 
    } 

    workoutStart = workoutStart.AddDays(7); 
} 
+0

所以你的意思是我必须做另一个循环?在第一个循环中没有其他方式吗? – n3tx 2012-08-10 07:09:36

+0

我试过这个解决方案,但我在哪里可以得到新的日期? – n3tx 2012-08-10 11:29:58

0

请考虑来检查我们的DateTime properties.Their值的值可能会低于您的SQL服务器允许日期时间字段。

  • 在分配“tmp”对象之前什么是updatedWorkWork对象的Date值?
  • 您的“旧”对象为空或日期的值是什么?

你的代码似乎没问题。该问题是DateTime属性和字段的值的基础。