DropDownList项目-免费时间



我想在我的DropDownList上。项目未占用小时数。

我的数据库中有一个表:

访问:visitID,trainerID,clientID,Data,Hour。

当客户想要添加新的就诊时,他必须选择培训师、日期并点击"检查可用时间"按钮。然后在下拉列表中。物品不应占用数小时。

例如,所有时间:9:00、10:00、11:00、12:00、13:00、14:00。表中有两个选定日期的就诊时间:9:00和12:00,因此客户在添加新就诊时应在DropDownList上查看免费时间:10:00、11:00、13:00、14:00。

此代码添加到DropDownList。数据库中的项目占用小时数:

using (myEntities dc = new myEntities())
{
   var hours = (from a in dc.Visits
                  where  (a.Data == TextBox1.Text && a.trainerID.Equals(DropDownList1.SelectedItem.Text))
                  orderby a.Hour
                  select new 
                  {
                      a.visitID,
                      a.Hour,
                  });        
    DropDownList2.DataSource = hours.ToList();
    DropDownList2.DataTextField = "Hour";
    DropDownList2.DataValueField = "visitID";
    DropDownList2.DataBind();
}

您说要显示可用小时的序列。您有一个函数,可以返回占用小时的集合。

正常情况下,可用小时数的集合等于除占用小时数之外的所有小时数的总和。正如预期的那样,提供此功能的函数被称为可枚举。除了

IEnumerable<Hour> allHours = GetAllHours();
IEnumerable<Hour> occupiedHours = RetrieveOccupiedHours();
IEnumerable<Hour> availableHours = allHours.Except(occupiedHours);

为了能够使用这个,你需要一个课时。为了使将来更改成为可能,可以考虑使用与小时不同的名称,例如appointmentPeriod。这允许您稍后以半小时或五十分钟或其他时间段更改预约期。

一旦你有了一个类来表示"小时"(预约期),函数GetAllHours将类似于:

IEnumerable<Hour> GetHoursOnDate(DateTime Date)
{
    // something difficult making sure that 3 o' clock in the middle of the night
    // is not considered as an available hour, nor the hours on Christmas day
    // and possible also not Sundays, Easter day, New Year's eve at 0:0 etc.
}
private void OnDropDown_ComboBoxAvailableHours(object sender, ...)
{
    DateTime selectedDate = GetSelectedDate();
    IEnumerable<Hour> hoursOnSelectedDate = GetHoursOnDate(selectedDate);
    IEnumerable<Hour> occupiedHoursOnSelectedDate = RetrieveOccupiedHours(date);
    IEnumerable<Hour> availableHoursOnSelectedDate = hoursOnSelectedDate
        .Except(occupiedHoursOnSelectedDate);
    comboBoxAvailableHours.Items.Clear();
    comboBoxAvailableHours.Items.AddRange(availableHoursOnSelectedDate
        .ToArray());
}

最新更新