Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:21:42+00:00 2026-05-13T21:21:42+00:00

I am using the asp.net mvc with the Entity Framework. I have a list

  • 0

I am using the asp.net mvc with the Entity Framework.
I have a list of appointments with a startedat field, an endedat field and a roomid field (called SpaceConfigurationId) and would like to find the list of appointments that have been double booked for a given room. It is fine to assume that endedat is always after startedat.

There are 4 cases to would allow an appointment to be matched:

  1. appointment a starts before appointment b starts and ends after appointment b starts and before appointment b ends
  2. appointment a starts after appointment b starts and before appointment b ends and ends after appointment b ends
  3. appointment a starts before appointment b starts and ends after appointment b ends
  4. appointment a starts after appointment b starts and before appointment b ends and ends after appointment b starts and before appointment b ends

I would like the list of appointments that meet any of those requirements. It seems simple but is actually a bit of a mind game. I had a look at intersect but didn’t seem to get anywhere.
I have the following variable defined and would like to return a list.

IQueryable<Appointment> appointments = Repository.ReadAppointments();
... insert code here ...
return appointments.ToList();

Here is some SQL which is really slow but may help to outline the problem

select COUNT(*)
from appointment a
    cross join appointment b
where
    not a.Id = b.Id
    AND
    a.SpaceConfigurationId = b.SpaceConfigurationId
    AND
    (
        (a.StartedAt < b.StartedAt and a.EndedAt > b.StartedAt and a.EndedAt < b.EndedAt)
        OR 
        (a.StartedAt > b.StartedAt and a.StartedAt < b.EndedAt and a.EndedAt > b.EndedAt)
        OR 
        (a.StartedAt < b.StartedAt and a.EndedAt > b.EndedAt)
        OR 
        (a.StartedAt > b.StartedAt and a.StartedAt < b.EndedAt and a.EndedAt > b.StartedAt and a.EndedAt < b.EndedAt)
    )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:21:43+00:00Added an answer on May 13, 2026 at 9:21 pm
    1. Define indexes on SpaceConfigurationId and date columns.
    2. There are 2 cases when appointments don’t match:

      1. When a ends before b.
      2. When a starts after b.

    So..

    select COUNT(*)
    from appointment a
    join appointment b
    on
        (not a.Id = b.Id)
        AND
        (a.SpaceConfigurationId = b.SpaceConfigurationId)
        AND
        NOT (a.EndetAt < b.StartedAt)
        AND
        NOT (a.StartedAt > b.EndetAt)
    

    So…

    repository.ReadAppointments().Where(a => repository.ReadAppointments().
         Any(b => 
             !(b.ID == a.ID) && 
             (a.SpaceConfigurationId == b.SpaceConfigurationId) &&
             !(a.EndetAt < b.StartedAt) &&
             !(a.StartedAt > b.EndetAt))).
         Select(t => t.ID).ToList();
    

    It doesn’t generate the same SQL. It actually uses EXISTS, but with proper indexes should work ok.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.