1

I have listed some doctor slots which are stored in a Dictionary as DateTime Key as Start Time and DateTime Value as End Time. I have another Dictionary with DateTime Key and DateTime Value to contain some booked or already scheduled times. Now how can I compare these two dictionaries to get my available or free slots? Code for doctor slots and booked slots are given below:

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

Now I would like to compare the doctorSlots dictionary with scheduledSlots to get the available slots from doctorSlots. The problem is scheduledSlots can have objects which does not have in the doctorSlots. In that case I need to compare each start and end time of scheduledSlots with doctorSlots. Can anyone give me suggestions how can I do this?

mnu-nasir
  • 1,642
  • 5
  • 30
  • 62
  • 2
    So basically you have a set of date ranges and almost anything you do with them will require iterating over them in order, which makes having them in a dictionary awkward. It would be better to create a `DateRange` type that's composed of a start and end date (or state date and timespan) then implement `IComparable` and store them in a sorted data structure like a `SortedSet` – juharr Aug 21 '21 at 19:51
  • 2
    Yeah... I agree with juharr - this data storage mechanism you've devised is awkward. Think about the calenadar (paper) on your wall. It shows a month as an array of slots and you can see "Holiday, Holiday, Holiday" next week on the Tue/Wed/Thu so you know you're not available for those slots. It is NOT a list of entries like "21st - dentist" "25th - 27th holiday", "28th piano recital" that must be searched when your buddy says "coming to my party on the 26th"? -> "hangon while I enumerate this list and work out if your 26th is between any of the ranges...". No,you just "lookup 26th..Oh..sorry" – Caius Jard Aug 21 '21 at 21:10

0 Answers0