用兩個dateTime(開始和結束日期時間)創建一個鍵。如果您想要輸入新的預訂,請檢查(例如使用linq)是否存在您的新開始日期和結束日期時間之間已知的結束日期時間,並且與已知的開始日期時間相同。如果沒有重疊,請添加它。
下面是一個例子:
Dictionary<TwoUintsKeyInfo,object> test = new Dictionary<TwoUintsKeyInfo, object>();
test.Add(new TwoUintsKeyInfo { IdOne = 3, IdTwo = 9 }, new object());
test.Add(new TwoUintsKeyInfo { IdOne = 10, IdTwo = 15 }, new object());
uint newStartPoint1 = 16,newEndPoint1=20;
bool mayUse = (from result in test
let newStartPointIsBetweenStartAndEnd = newStartPoint1.Between(result.Key.IdOne,result.Key.IdTwo)
let newEndPointIsBetweenStartAndEnd = newEndPoint1.Between(result.Key.IdOne,result.Key.IdTwo)
let completeOverlap = result.Key.IdOne < newStartPoint1 && result.Key.IdTwo > newEndPoint1
let oldDateWithingNewRange = result.Key.IdOne.Between(newStartPoint1, newEndPoint1) || result.Key.IdTwo.Between(newStartPoint1, newEndPoint1)
let FoundOne = 1
where newStartPointIsBetweenStartAndEnd || newEndPointIsBetweenStartAndEnd || completeOverlap || oldDateWithingNewRange
select FoundOne).Sum()==0;
使用:
public static class LinqExtentions
{
/// <summary>
/// Note: For the compare parameters; First the low, than the High
/// </summary>
/// <returns>Bool</returns>
public static bool Between<T1, T2, T3>(this T1 actual, T2 lowest, T3 highest)
where T1 : IComparable
where T2 : IConvertible
where T3 : IConvertible
{
return actual.CompareTo(lowest.ToType(typeof(T1), null)) >= 0 &&
actual.CompareTo(highest.ToType(typeof(T1), null)) <= 0;
}
}
public class TwoUintsKeyInfo
{
public uint IdOne { get; set; }
public uint IdTwo { get; set; }
public class EqualityComparerTwoUintsKeyInfo : IEqualityComparer<TwoUintsKeyInfo>
{
public bool Equals(TwoUintsKeyInfo x, TwoUintsKeyInfo y)
{
return x.IdOne == y.IdOne &&
x.IdTwo == y.IdTwo;
}
public int GetHashCode(TwoUintsKeyInfo x)
{
return (Math.Pow(x.IdOne, Math.E) + Math.Pow(x.IdTwo, Math.PI)).GetHashCode();
}
}
}
我測試了,似乎是工作。 祝你好運!
可以使數據時 –
你需要防止重疊的組合鍵?在您的示例中,我是否可以在下午6:00至8:00之間創建07/08/2013的條目? – DavidN
在你的'string'的例子中,你使用什麼樣的*作爲關鍵字? –