Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用日期值计算元素_C#_Linq_Linq To Sql - Fatal编程技术网

C# 使用日期值计算元素

C# 使用日期值计算元素,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我有一个DB表,叫做DBAppoints。该数据库由两个字段组成:开始日期和结束日期。我想做的是计算在某个时间间隔内我有多少约会。例如,我的表中有两个约会: App. 1 : 8h30 -> 10h30 App. 2 : 9h -> 10h 更明确地说,我真正做的很简单:我想根据开始日期和持续时间插入一个新约会。要执行此操作,我必须检查是否可以从某个日期和时间添加约会。再举一个例子: 我想增加一个约会,其持续时间等于2小时。根据我的记录,我会一步一步地用半个小时来决定我能不能做到

我有一个DB表,叫做DBAppoints。该数据库由两个字段组成:开始日期和结束日期。我想做的是计算在某个时间间隔内我有多少约会。例如,我的表中有两个约会:

App. 1 : 8h30 -> 10h30
App. 2 : 9h -> 10h
更明确地说,我真正做的很简单:我想根据开始日期和持续时间插入一个新约会。要执行此操作,我必须检查是否可以从某个日期和时间添加约会。再举一个例子:

我想增加一个约会,其持续时间等于2小时。根据我的记录,我会一步一步地用半个小时来决定我能不能做到

  • 8h30到10h30:不可能,已经有2个约会了
  • 9小时到11小时:仍然存在相同的问题,预约不能超过2次
  • 9h30到11h30:这里的情况也一样
  • 10小时到12小时:第二次约会结束后,我可以在这里添加一个新约会
  • 所以,要做到这一点,我在代码中就是这么做的:

    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);
                        }
                    }
    
    dbappoint[]appointsofCurrentDay=(从context.dbappoints中的a.StartDate.Value.Date==day.Date选择a.ToArray();
    foreach(任命中的dbapp)
    {
    DateTime end=dbapp.PatchExpiration.Value;
    
    对于(DateTime startTime=dbapp.StartDate.Value;startTime a.StartDate我更改了代码,可能是这样的:

    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)));
    
    int countAppointment=appointmentsOfCurrentDay.Count(a=>(startTime>=a.StartDate&&startTimea.StartDate&&startTime.AddHours(duration)=startTime&&a.StartDatestartTime&&a.EndDate只看:
    
    int countAppointment=appointsofCurrentDay.Count(a=>a.StartDate我写了这个小例子来解释我的评论,希望它能帮助解决您的问题。很抱歉懒得重新编写代码。如果您需要进一步的帮助,请告诉我

       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;
        }
    
    private List ValidEndTime=new List();//包含validEndDate的列表
    私有无效按钮1\u单击(对象发送者,事件参数e)
    {
    List currentAppointment=新列表();
    currentAppointment.Add(转换为ToDateTime(“2014年5月22日14:22”);
    currentAppointment.Add(转换为ToDateTime(“2014年5月22日14:30”);
    foreach(当前约会中的日期时间结束日期)
    {
    如果(IsAppPointmentTimeValid(endDate)==true)
    {
    有效时间。添加(结束日期);
    }    
    }     
    }
    private bool IsAppointmentTimeValid(DateTime newAppointmentEndTime)
    {
    //比较max(maxendDateTime)和newAppointmentDateTime
    返回ValidEndTime.Max(t=>t.Date)
    尝试获取当天的最长(结束时间),然后在最长(结束时间)之后开始任何其他约会。我没有真正理解你的意思。你能发布一个答案吗?我更改了答案,请检查一下,知道吗。
    test: 9h00 <= 9h00 && 11h <= 10h00 (false)
    
       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;
        }