DropDownList项目 - 免费小时

时间:2016-05-06 08:30:46

标签: asp.net linq webforms

我希望我的DropDownList.Items没有占用时间。

我的数据库中有一张表:

  

访问:visitID,trainerID,clientID,数据,小时。

当客户想要添加新访问时,他必须选择培训师,日期并点击按钮"检查可用时间"。然后在DropDownList.Items上不应占用小时数。

例如 - 所有时间: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.Items占用数小时的小时数:

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();
}

1 个答案:

答案 0 :(得分:0)

您说您想要显示可用小时的顺序。你有一个函数可以返回占用时间的集合。

通常,可用小时数的集合等于除占用小时数之外的所有小时的集合。正如预期的那样,为您提供此功能的功能称为 Enumerable.Except

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());
}
相关问题