我正在尝试将日期更改为列表中的所有锻炼,其他属性工作正常,仅当我更改日期时才会出现问题。我尝试了不同的故障排除,但没有一个有效。我尝试使用updatedWorkout.Date作为workoutStart =超出范围。如果我使用old.Date,那么,如何添加7days的新日期?
也许有更好的方法可以做到这一点?
这是我的方法:
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 for help!
答案 0 :(得分:0)
我认为你可以使用循环7。
像这样:var workoutStart = workout.Date;
while(true){
if(workoutStart <= schedule.ToDate){
// Do something
}else{
break;
}
workoutStart = workoutStart.AddDays(7);
}
答案 1 :(得分:0)
请考虑检查DateTime属性的值。它们的值可能小于SQL Server允许的datetime字段。
您的代码似乎没问题。该问题是DateTime属性和字段值的基础。