C#应用程序如何为此实现字典或哈希表?

这是我的问题,我想写一个基本的控制台应用程序,我在其中输入日期作为输入,如果该日期尚未输入应用程序然后允许时间输入注释,即2013年7月7日时间5:00 – 晚上7点输入文字等等

然后应用程序将保持循环,如果我输入相同的日期,我不能输入与上面相同的时间,但我应该能够输入例如7:00到8。

我在考虑使用字典:

Dictionary BookingDict = new Dictionary(); 

并将日期添加为id,但似乎只能输入一个元素id

可以请一些人帮忙

你必须使用列表或字典吗? 如果你不会在其中搜索很多并且没有很多元素,那么最好使用通用列表: List

然后,您可以使用LINQ搜索列表以检索所需的预订。

我会推荐一个字典,其中键是日期,值是24个布尔代表小时的数组。 只有预订是整整一小时。 如果它是按分钟,那么arrays将太大,然后可能需要另一个选项。

如果小时被预订,则数组中的每个单元格都为真,例如,如果<07/08/13,则预订[2] =真>意味着预订了在07/08/13预订的小时2-3。

当你得到一个新的预订时,你需要检查两者之间的每个小时。 如果您有4-10小时,则需要检查4,5,6,7,8,9中的值。 我认为不是最好的效率,但如果没有人预订整整一周,那就没那么糟糕了。

总结我的答案是使用

 Dictionary bookingTbl 

使用两个dateTime(开始和结束日期时间)创建一个键。 如果要输入新的预订,请检查(例如,使用linq)是否已知新的开始和结束日期时间之间的结束日期时间,并对已知的开始日期时间执行相同操作。 如果没有重叠,请添加它。

这是一个例子:

 Dictionary test = new Dictionary(); 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 { ///  /// Note: For the compare parameters; First the low, than the High ///  /// Bool public static bool Between(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 { 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(); } } } 

我测试过,似乎工作正常。 祝好运!