Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The fact that the intervals start and end point is correct, do not check it. We examine the existing interval intersects whether the interval in question:</p> <pre><code>void Main() { var ExistIntervals = new HashSet&lt;Interval&gt;(); //1 Aug 2012 4 Aug 2012 //5 Aug 2012 11 Aug 2012 //12 Aug 2012 15 Aug 2012 ExistIntervals.Add(new Interval { From = new DateTime(2012, 8, 1), To = new DateTime(2012, 8, 4) }); ExistIntervals.Add(new Interval { From = new DateTime(2012, 8, 5), To = new DateTime(2012, 8, 11) }); ExistIntervals.Add(new Interval { From = new DateTime(2012, 8, 12), To = new DateTime(2012, 8, 15) }); var QueryIntervals = new HashSet&lt;Interval&gt;(); //1 Aug 2012 2 Aug 2012 INVALID //5 Aug 2012 11 Aug 2012 INVALID //10 Aug 2012 10 Aug 2012 VALID //15 Aug 2012 15 Aug 2012 INVALID QueryIntervals.Add(new Interval { From = new DateTime(2012, 8, 1), To = new DateTime(2012, 8, 2) }); QueryIntervals.Add(new Interval { From = new DateTime(2012, 8, 5), To = new DateTime(2012, 8, 11) }); QueryIntervals.Add(new Interval { From = new DateTime(2012, 8, 10), To = new DateTime(2012, 8, 10) }); QueryIntervals.Add(new Interval { From = new DateTime(2012, 8, 15), To = new DateTime(2012, 8, 15) }); var result = QueryIntervals.Where( x=&gt; !ExistIntervals.Any( y=&gt;(y.From &lt;= x.From &amp;&amp; x.From &lt;= y.To) || (y.From &lt;= x.To &amp;&amp; x.To&lt;=y.To) ) ); result.Dump(); } public class Interval { public DateTime From { get; set; } public DateTime To { get; set; } } </code></pre> <p><strong>The result is an empty set.</strong></p> <p>(the result is an empty set, since 10 Aug 2012th in this interval: 5 Aug 2012 ------ 11 Aug 2012)</p> <p>You can test with <a href="http://www.linqpad.net/" rel="nofollow">LinqPad</a>.</p> <p><strong>Update</strong>:</p> <p>Suppose that the table name "CompaitnDates", the context name "CompaitnContext" and to determine the interval of two variables: "checkFrom", "checkTo".</p> <pre><code>var checkFrom = new DateTime(2012, 8, 10); var checkTo = new DateTime(2012, 8, 10); var db = new CompaitnContext(); </code></pre> <p>then the test may be as follows:</p> <pre><code>var isValid = !db.CompaitnDates.Any( y=&gt;(y.From &lt;= checkFrom &amp;&amp; checkFrom &lt;= y.To) || (y.From &lt;= checkTo &amp;&amp; checkTo&lt;=y.To) ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload