首页 > 解决方案 > 将两个字典与 DateTime Key 和 DateTime Value 进行比较以获取 C# 中可用的医生插槽

问题描述

我列出了一些存储在字典中的医生插槽,作为开始时间的日期时间键和作为结束时间的日期时间值。我有另一个带有 DateTime 键和 DateTime 值的字典来包含一些预定的或已经预定的时间。现在我如何比较这两个字典来获得我的可用或免费插槽?医生名额和预定名额代码如下:

private Dictionary<DateTime, DateTime> GetDoctorSlots()
{
    var doctorSlots = new Dictionary<DateTime, DateTime>();

    var startTime = DateTime.UtcNow.Date.Date + new TimeSpan(9, 0, 0);
    var endTime = DateTime.UtcNow.Date.Date + new TimeSpan(23, 30, 0);

    var start = startTime;

    while (start <= endTime)
    {
        var end = start.AddMinutes(30);
        doctorSlots.Add(start, end);
        start = end;
    }

    return doctorSlots;
}
    
private Dictionary<DateTime, DateTime> GetScheduledSlots(DateTime studyDate, int? orgId)
{            
    var appointments = (from schedule in _context.PmrSchedules
                        join scheduleDtl in _context.PmrScheduledtls on schedule.ScheduleId equals scheduleDtl.ScheduleId
                        where schedule.IsActive == true
                            && scheduleDtl.IsActive == true
                            && schedule.ScheduleDt.Year == studyDate.Year
                            && schedule.ScheduleDt.Month == studyDate.Month
                            && schedule.ScheduleDt.Day == studyDate.Day
                            && schedule.OrgId == orgId
                        select scheduleDtl).ToList();

    var scheduledSlots = new Dictionary<DateTime, DateTime>();

    foreach (var schedule in appointments)
    {
        if (schedule.StartTime == null || schedule.EndTime == null)
            continue;

        var start = (DateTime)schedule.StartTime;
        var end = (DateTime)schedule.EndTime;

        scheduledSlots.Add(start, end);
    }

    scheduledSlots = scheduledSlots.OrderBy(x => x.Key.Date).ThenBy(a => a.Key.Hour).ToDictionary(a => a.Key, a => a.Value);            

    return scheduledSlots;
}

private Dictionary<DateTime, DateTime> GetAvailableSlots(DateTime studyDate, int? orgId)
{
    var doctorSlots = GetDoctorSlots();
    var scheduledSlots = GetScheduledSlots(studyDate, orgId);

    // COMPARE doctorSlots WITH scheduledSlots

    return RESULT;
}
    

现在我想将医生插槽字典与预定插槽进行比较,以从医生插槽中获取可用插槽。问题是scheduleSlots可以包含医生插槽中没有的对象。在这种情况下,我需要将 scheduleSlots 的每个开始和结束时间与医生插槽进行比较。谁能给我建议我该怎么做?

标签: c#dictionarydatetimedatetime-comparison

解决方案


推荐阅读