Search code examples
c#.neticalendarical-dotnet

What's the best way to shrink a calendar to only contain the necessary calendar objects depending on a start and end date


Using iCal.Net.

What I'm trying to achieve, is to shrink a calendar to send it over the internet, so I don't have to send the whole calendar, which would be wasteful (considering it can get big) if I'm asked what happens in a single day.

We should only send what occurs in the given period.

Calendar.GetOccurrences(IDateTime startTime, IDateTime endTime)

Seems to be a good start, I'm not sure how to get the actual CalendarObject then, nor if it's the good approach.


Solution

  • That's a use case I haven't encountered before, but it shouldn't be too difficult. I think I'd do something like this:

    var relevantEvents = bigCalendar.GetOccurrences(start, end)
        .Select(o => o.Source)  // The parent IRecurrable
        .Cast<Event>()          // Which is an Event/CalendarEvent
        .Distinct()
        .ToList();
    
    var smallerCalendar = new Calendar();
    
    // I should add an extension method to UniqueComponentList for AddRange()
    foreach (var relevantEvent in relevantEvents)
    {
        smallerCalendar.Events.Add(relevantEvent);
    }