該區間的開始和結束點是正確的事實衝突,不檢查。我們檢查現有的間隔相交是否有問題的區間:
void Main()
{
var ExistIntervals = new HashSet<Interval>();
//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<Interval>();
//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=> !ExistIntervals.Any(
y=>(y.From <= x.From && x.From <= y.To)
|| (y.From <= x.To && x.To<=y.To)
)
);
result.Dump();
}
public class Interval
{
public DateTime From { get; set; }
public DateTime To { get; set; }
}
結果是空集。
(結果是一個空集,自10 8 2012th在此區間:2012年8月5日------ 2012年8月11日)
可以與LinqPad測試。
更新:
假設表名 「CompaitnDates」,上下文名稱 「CompaitnContext」,並確定兩個變量的時間間隔: 「checkFrom」, 「checkTo」。
var checkFrom = new DateTime(2012, 8, 10);
var checkTo = new DateTime(2012, 8, 10);
var db = new CompaitnContext();
那麼測試可能如下:
var isValid = !db.CompaitnDates.Any(
y=>(y.From <= checkFrom && checkFrom <= y.To)
|| (y.From <= checkTo && checkTo<=y.To)
);
當它是有效的,當它不是我不明白。 –
請分享一些代碼給我們來幫助您 –
您能否重新說明您的問題或提供一些額外的信息/代碼?我不明白你在問什麼。 – Drasive