对具有日期值的元素进行计数

本文关键字:元素 日期 | 更新日期: 2023-09-27 18:08:13

我有一个名为DBAppointments的DB表。该数据库由2个字段组成:Start Date和End Date。我要做的是计算在一定的时间间隔内有多少个约会。例如,我的表中有2个约会:

App. 1 : 8h30 -> 10h30
App. 2 : 9h -> 10h

更明确地说,我真正做的事情非常简单:我想根据开始日期和持续时间插入一个新的约会。要执行此操作,我必须检查是否可以添加特定日期和时间的约会。再举一个例子:

我想添加一个持续时间为2小时的约会。根据我的记录,我将每隔半个小时来决定我是否可以。

  1. 8时30分至10时30分:不可能,已经有2个约会
  2. 9h至11h:问题依然存在,不能超过2次预约
  3. 9时30分至11时30分:此处情况相同
  4. 10h到12h:我可以在这里添加一个新的约会,因为第二个约会已经完成了!

要做到这一点,我在代码中就是这么做的:

DBAppointment[] appointmentsOfCurrentDay = (from a in context.DBAppointments where a.StartDate.Value.Date == day.Date select a).ToArray();

            foreach (DBAppointment dbapp in appointments)
            {
                DateTime end = dbapp.PatchExpiration.Value;
                for (DateTime startTime = dbapp.StartDate.Value; startTime <= end; startTime = startTime.AddHours(0.5))
                {
                    **int countAppointment = appointmentsOfCurrentDay.Count(a => a.StartDate <= startTime && startTime.AddHours(duration) <= a.EndDate);**
                    if (countAppointment < maxBeds && (Math.Ceiling(Decimal.ToDouble(dbapp.PatchQuantity.Value)) - Decimal.ToDouble(dbapp.PatchQuantity.Value) >= patchQuantity))
                    {
                        IntervalViewModel ivm = new IntervalViewModel();
                        ivm.StartDate = startTime;
                        ivm.EndDate = startTime.AddHours(duration);
                        listValidIntervals.Add(ivm);
                    }
                }

我的问题在于粗体显示的代码行。我们在8点30分开始,10点30分结束,所以我的计数等于2,这是正确的。但是,当我们执行第二次迭代时(因此startime等于9h),计数显示为1,这是不正确的。

我明白为什么(这是因为我们说a.s startdate大于或等于startime),但我不知道如何修复它

对具有日期值的元素进行计数

看看:int countAppointment = appointmentsOfCurrentDay.Count(a => a.StartDate <= startTime && startTime.AddHours(duration) <= a.EndDate);

应该是:int countAppointment = appointmentsOfCurrentDay.Count(a => a.StartDate <= startTime && a.EndDate >= startTime.AddHours(duration));

之前您有:

  • 第一次迭代:ivm.StartDate = 8h30 ; ivm.StartDate.AddHours(2) = 10h30

    test : 8h30 <= 8h30 && 10h30 <= 10h30 (true)
    
  • 第二次迭代:ivm.StartDate = 9h00 ; ivm.StartDate.AddHours(2) = 11h00

    test: 9h00 <= 9h00 && 11h <= 10h00 (false)
    

我改变了我的代码,可能是这样的:

int countAppointment = appointmentsOfCurrentDay.Count(a => (startTime >= a.StartDate && startTime < a.EndDate || startTime.AddHours(duration) > a.StartDate && startTime.AddHours(duration) <= a.EndDate) ||
                                                    (a.StartDate >= startTime && a.StartDate < startTime.AddHours(duration) || a.EndDate > startTime && a.EndDate <= startTime.AddHours(duration)));

我写了这个小例子来解释我的评论,希望它能帮助解决你的问题。很抱歉懒得重写代码。如果你需要进一步的帮助,请告诉我

   private List<DateTime> ValidEndTime = new List<DateTime>(); //Contains the list of validEndDate 
    private void button1_Click(object sender, EventArgs e)
    {
        List<DateTime> currentAppointment = new List<DateTime>();
        currentAppointment.Add(Convert.ToDateTime("22 May 2014 14:22"));
        currentAppointment.Add(Convert.ToDateTime("22 May 2014 14:30"));
        foreach (DateTime endDate in currentAppointment)
        {
            if (IsAppointmentTimeValid(endDate) == true)
            {
                ValidEndTime.Add(endDate);
            }    
        }     
    }
    private bool IsAppointmentTimeValid(DateTime newAppointmentEndTime)
    {
        //compare max(maxendDateTime) with the newAppointmentDateTime
        return ValidEndTime.Max(t => t.Date) < newAppointmentEndTime? true : false;
    }