title: Checking for date overlap across multiple date range objects
body: <p>I have several records in a database that have Start and End Dates</p>
  
  <pre><code>09/15/2011 - 09/30/2011
  10/15/2011 - 10/22/2011
  11/01/2011 - 11/15/2011
  </code></pre>
  
  <p>When user stores a record, I need to make sure dates don't overlap.
  My simple code checks date ranges within a specific record (e.g. user enters 9/16/2011  or 10/21/2011, I throw an exception.)</p>
  
  <p>But, on the slim chance a user gets creative (e.g. 10/14/2011 - 10/23/2011 or even 10/14/2011 to 11/16/2011), now they have circumvented my check.</p>
  
  <p>BTW, the user could enter 10/14/2011 to 10/23/2011 if they were editing the record that contained values 10/15/2011 - 10/22/2011.</p>
  
  <p>So, I'm trying to solve this riddle with a linq query.  However, what I have isn't working exactly right.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong> Nevermind about code not working.  While trying to provide an example to expand on Miika's repsonse, I found my answer.  So, giving credit to Miika for pointing me in the right direction and posting my working code below:</p>
  
  <p>Here's my code:</p>
  
  <pre><code>        Private Sub CheckForOverlap(myMonth As Messages.MyMonth)
  
  
                          Dim am As New MyMonth()
                          Dim amCollection As Messages.MyMonthCollection
                          Dim overlappingMyMonthDate As Boolean = False
                          Dim sErrorMsg As String = ""
  
  '...non-applicable code omitted
  
      Dim query = From s In amCollection _
                                   Let s1 As MyMonth = CType(s, MyMonth) _
                                   Where s1.AttendanceMonthID &lt;&gt; attendanceMonth.AttendanceMonthID And _
                                   (CDate(attendanceMonth.StartDate) &lt; CDate(s1.StartDate) And CDate(attendanceMonth.EndDate) &gt; CDate(s1.EndDate)) _
                                   Select s1
  
                  If query.Count &gt; 0 Then
                      sErrorMsg = "Dates entered surround another entry"
                  End If
  
                          If overlappingMyMonthDate Then
                              Throw New Exception(sErrorMsg)
                          End If
  
                      End Sub
  
  
                  End Class
  </code></pre>
  
  <p>It all came down a LINQ query.</p>