2013-05-11 41 views
0

我正在尝试预约系统。唯一的问题是,如果接受另一个约会,我不想让客户创建约会。使用DateTime实例比较2次

我要离开预约之间1小时和另一个说,如果任命一个是在12:00你12:00和13:00

这里与着书任命是我的代码:

List<Appointment> acceptedAppointments = new Service1Client().getAllAcceptedAppointments(); 

获得所有接受的约会。

foreach (Appointment item in acceptedAppointments) 
      { 
       if (item.Appointment_DateTime.Date == myDate.Date) 
       { 
        if (myDate.AddHours(1) > item.Appointment_DateTime) 
        { 

        } 
       } 
      } 

我不知道到底是什么,我需要在这里做,如果有人能够帮助这将是巨大的感谢!

回答

2
bool isValidAppointment = true; 

// Go through all accepted appointments 
foreach (Appointment item in acceptedAppointments) 
{ 
    // Check if the difference between the appointments is less than 60 minutes 
    if (item.Appointment_DateTime.Substract(myDate).Duration.TotalMinutes < 60) 
    { 
     // If so, set bool to indicate invalid appointment and stop validation 
     isValidApopintment = false; 
     break; 
    } 
} 

if (isValidAppointment) 
{ 
    // Handle valid appointment 
} 
else 
{ 
    // Handle invalid appointment 
} 

这可以简化为:

bool isValidApointment = acceptedAppointments.Any(x => x.Substract(myDate).Duration.TotalMinutes < 60);