1

I'm wondering if anyone can help me? I'm trying to prevent events from overlapping in a room booking system. Each event has a start date/time and an end date/time. What I want to happen is if a user tries to book an event between times that already exist for an event it should error.

So for example the following should error as they overlap:-

Event 1:
- Start Date/Time: 10/08/2012 09:00
- End Date/Time: 10/08/2012 11:00

Event 2:
- Start Date/Time: 10/08/2012 10:00
- End Date/Time: 10/08/2012 12:00

I'm using MVC and Entity Framework, so a Linq solution would be preferable.

Any help would be much appreciated :)

tereško
  • 58,060
  • 25
  • 98
  • 150
Vince Ashby-Smith
  • 1,152
  • 4
  • 18
  • 36

1 Answers1

3

Do you mean something like this:

new List<Event>()
{
    new Event() { StartsAt = DateTime.Now.AddHours(-1), EndsAt = DateTime.Now.AddHours(1) } 
};

Event newEvent = new Event() { StartsAt = DateTime.Now.AddHours(-3), EndsAt = DateTime.Now.AddHours(-2) };
bool newEventBookable = !existingsEvents.Any(x => x.StartsAt <= newEvent.EndsAt && x.EndsAt >= newEvent.StartsAt);

You can then just throw an error if newEventBookable is false.

I think this logic is OK for prior, subsequent, overlapping and contained events - but I'll soon find out if it it's not!

dommer
  • 19,610
  • 14
  • 75
  • 137