创建具有两个日期时间(开始和结束日期时间)的键。当您要输入新的预订时,请检查(例如使用 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();
}
}
}
我测试了它,似乎可以正常工作。祝你好运!