Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在日期范围内匹配的字典键_C#_Collections_Dictionary - Fatal编程技术网

C# 在日期范围内匹配的字典键

C# 在日期范围内匹配的字典键,c#,collections,dictionary,C#,Collections,Dictionary,我想将数据存储在通用字典中,其中的键与日期范围匹配 例如,我提出了以下想法 public class MyKey : IEquatable<MyKey> { public int Key { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public override int GetHashCode() { retu

我想将数据存储在通用字典中,其中的键与日期范围匹配

例如,我提出了以下想法

public class MyKey : IEquatable<MyKey> 
{
  public int Key { get; set; }
  public DateTime StartDate { get; set; }
  public DateTime EndDate { get; set; }

  public override int GetHashCode() 
  { 
    returns Key;
  }

  // if there is overlap in date range consider them equal
  public bool Equals(MyKey other)
  {
    if (Key!=other.Key)
      return false;
    else if(other.StartDate >=StartDate && other.StartDate <=EndDate) 
      return true;
    else if(other.EndDate >=StartDate && other.EndDate <=EndDate) 
      return true;
    else if(StartDate >=other.StartDate && StartDate <=other.EndDate) 
      return true;
    else if(EndDate >=other.StartDate && EndDate <=other.EndDate) 
      return true;
    else
      return false;
  }
}
公共类MyKey:IEquatable
{
公共int密钥{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间结束日期{get;set;}
公共覆盖int GetHashCode()
{ 
返回密钥;
}
/如果日期范围有重叠,则考虑它们相等。
公共布尔等于(MyKey其他)
{
if(Key!=其他.Key)
返回false;

else如果(other.StartDate>=StartDate&&other.StartDate=StartDate&&other.EndDate=other.StartDate&&StartDate=other.StartDate&&EndDate您的Equals实现违反了。尤其是您的实现不满足传递性规则:

  • 如果
    x.Equals(y)&&y.Equals(z)
    返回true,则
    x.Equals(z)
    返回true
违反此准则是一个坏主意,可能会导致问题和混乱。我建议您不要这样做

我会避免将间隔作为键存储在字典中。如果愿意,可以将特定键的间隔列表作为值放在字典中,但它不应该是键的一部分

搜索以查找间隔时,可以首先使用dictionary键获取该键的间隔列表,然后迭代这些间隔以查找与参数重叠的间隔。如果特定键的间隔不重叠,则可以对其进行排序并使用二进制搜索以查找特定间隔。如果特定键的tervals可以重叠,您可以查看其他数据结构,例如

相关问题


对于getHashCode,返回
this.StartDate
this.EndDate
this.Key
的散列值。有关简单的散列方法,请参阅。有更好的散列方法,但该页面应该可以让您开始

添加一个静态方法,该方法接受一个int键和一个DateTime,因为这似乎就是您使用它的方式。(如果需要,可以为一个范围接受两个DateTime)
returnmydictionary[myDictionary.Keys.Find(x=>valuex.StartDate)&&x.Key=Key];

两个日期:
returnmydictionary[myDictionary.Key.Find(x=>((date1x.StartDate))| |(date2x.StartDate)))&&x.Key=Key];

如果您使用的是一个范围,则其中任何一个都可以成为“全部查找”,因为可能会发生碰撞

将您的equals条件更改为

返回this.StartDate.Equals(o.StartDate)和this.EndDate.Equals(o.EndDate)和this.Key.Equals(o.Key);


使用上述静态方法,您不再需要重叠键的概念。

与此问题非常类似:您不需要字典。您需要间隔树()或间隔跳过列表数据结构。我在equals比较中漏掉了一行。如果键本身不匹配,equals将返回false。因此我不认为这违反了您在上面关于哈希代码的规则。对此感到抱歉。@Jeff:根据您对问题的更新,我对我的答案做了很多更改。谢谢,Mark。我认为区间树是不合适的,因为主要查找是基于一个键的,在大多数情况下不会有其他的区间。传递性规则很有趣。我不确定如果MyKey仅被设计为字典的键,违反这一规则的含义是什么。在这一点上,我似乎必须要么切换到list类型结构,只需搜索匹配项或将我存储在字典中的对象更改为同一个键在不同范围内的对象集合。我忽略了键也必须相等。将此添加到编辑的问题中?Jack,这种静态查找方法似乎可行,但我们似乎没有真正利用它字典的散列性质非常重要,因为我们将迭代键以找到匹配项。但这似乎是最简单的实现方法。我接受了这个答案,因为我最终实现了它。间隔列表似乎不起作用,因为在大多数情况下,键是最重要的。
var dict = new Dictionary<MyKey,MyClass>();
Populate(dict);

// get an element where the current date is in the daterange of the key
// in the collection
var key = new MyKey();
key.Key=7;
key.StartDate=DateTime.Now;
key.EndDate=key.StartDate;

// retrieve the matching element for the date
var myclass = dict[key];