I have a list of events and now I want to find out which events do overlap. Below you find the code I currently have, but I've the problem that the item which is searched for is also included in the list.
List<SomeEventObject> overlappingEvents = new List<SomeEventObject>();
foreach (SomeEventObject eventItem in EventList)
{
bool overlapping = false;
foreach (SomeEventObject anotherEventItem in EventList)
{
if (eventItem.StartDate <= anotherEventItem.EndDate &&
eventItem.EndDate >= anotherEventItem.StartDate)
{
overlapping = true;
overlappingEvents.Add(anotherEventItem);
}
}
if (overlapping)
overlappingEvents.Add(eventItem);
}
I would need to create a new list without the item searched for. Therefore I'm asking if there is a nice LINQ expression which can handle that for me. This is some pseudo code I thought of:
EventList.Where(e =>
eventItem.StartDate <= e.EndDate &&
eventItem.EndDate >= e.StartDate);
In this context eventItem doesn't exist of course.
As a result I think I would need two lists: one with overlapping events and one with non-overlapping events. But that should be possible with .Except()
if I have my overlapping event list.
Edit:
I've created a dotnetfiddle so that one can play with it. One important question is the overlapping algorithm.
Event 1:
StartDate: today, 10:00
EndDate: today, 10:05
Event 2:
StartDate: today, 10:05
EndDate: today, 10:10
If you present this to the user then this is NOT overlapping. So I've to revise my algorithm.