Skip to content

Working with collections of calendar events

Rian Stockbower edited this page Nov 24, 2017 · 3 revisions

Extract a range of calendar events from a calendar

var relevantEvents = bigCalendar.GetOccurrences(start, end)
    .Select(o => o.Source)
    .Cast<CalendarEvent>()
    .Distinct()
    .ToList();

var smallerCalendar = new Calendar();
smallerCalendar.Events.AddRange(relevantEvent);

Incidentally, if you need to do something like this in your application, you may wish to read the concurrency page for information on how to spread the work across all your CPU cores.

Working with sets of events

The easiest way to do this is to create a HashSet<CalendarEvent> for each collection, and do set comparisons.

Suppose oldCollection contains some number of CalendarEvents. newCollection contains all of the CalendarEvents in oldCollection, plus a few more. You want to find the new events that have been added:

var completeSet = new HashSet<CollectionEvent>();
completeSet.UnionWith(newCollection);  // Add the new events
completeSet.ExceptWith(oldCollection); // Subtract the old

This will leave you with only the CalendarEvents that have been added since oldCollection was written down.