【问题标题】:compare DateTime of sorted dictionary with DateTime field将排序字典的 DateTime 与 DateTime 字段进行比较
【发布时间】:2018-04-13 00:13:24
【问题描述】:

我有一个需要由排序字典初始化的数组。该数组将包含已在字典中修改的整数值。该数组的目的是包含在特定日期完成的提交。使用排序后的字典,我将 DateTime 作为键,将整数作为值。

    //Array of integers which contains the number of commits that have been done on a particular day
    protected int[] Values;

    //Initializing a new SortedDictionary
    private SortedDictionary<DateTime, int> Sorted = new SortedDictionary<DateTime, int>();

现在我用天(需要完成分配的时间段)和每天的起始整数值“0”(在类构造函数内部)填充排序字典。

        //Fill Sorted dictionary with days and starting 0 value (inside class constructor)
        for (DateTime date = assignment.Start; date < assignment.End ; date = date.AddDays(1))
        {
            Sorted.Add(date, 0);
        }

接下来,我有几个来自 CommitInfo 类的提交。这些提交包含一个字段 TimeStamp,它是一个“日期时间”值。此提交的日期必须与排序字典中的 DateTime 键进行比较。如果日期相等,则字典中特定键的值需要加 1。

        //Go through all the CommitInfo values
        foreach(CommitInfo dates in commits)
        {
            //For every CommitInfo value go through all of the Keypairs inside the dictionary
            foreach(KeyValuePair<DateTime, int> kvp in Sorted)
            {
                //If the dates are the same, increment by 1.
                if (Sorted.ContainsKey(dates.TimeStamp.Date))
                {
                    Sorted[kvp.Key] += 1;
                }
            }
        }

现在这里可能出错了。字典包含应有的日期,但是当提交日期与字典中的日期相同时,每天的整数“0”值不会增加。我也尝试过这种 if 结构,但它也不起作用:

                if (dates.TimeStamp.Date == kvp.Key)
                {
                    Sorted[kvp.Key] += 1;
                }

为了提供完整的信息,因为它可能在其他地方出错(即使我进行了调试),下一步是初始化并填充“值”数组。使用以下代码完成:

        Values = new int[Sorted.Count];
        Values = Sorted.Values.ToArray();

有人可以帮助我如何让它正常工作吗?因为我尝试了很多东西,但都没有奏效。

【问题讨论】:

  • 您确定要跳入 if 语句吗?您只使用密钥检查 dates.TimeStamp 的日期部分。有你的任务。开始一个时间段?如果是这样,那么您的密钥也是如此,并且该语句将是错误的。
  • 尝试Sorted.Add(date.Date, 0); 以确保您没有比较时间组件。
  • @Link 我 99% 确定问题出在 if 结构中。但是当我执行 'assignment.Start.Date' 或 Sorted.Add(date.Date, 0) 或 'kvp.Key.Date 我得到一个 InvalidOperationException。
  • @Lor 你能提供一个minimal, verifiable and complete example吗?这会更容易理解。
  • @Link 我已经解决了这个问题。我会发布答案:) 无论如何感谢您的帮助!

标签: c# datetime dictionary compare commit


【解决方案1】:

我在您的原始代码中看到的唯一一件事是,当您循环提交时,对于每个提交,您还循环通过字典键,这是不必要的。您需要做的就是,对于每次提交,查看是否dictionary.ContainsKey(commit.Timestamp)。如果是,那么您可以直接使用dictionary[commit.Timestamp] += 1;

如果下面的代码不适用于您的数据,则可能是数据有问题。如果您遇到任何错误,请告诉我,我会尽力提供帮助。

我会这样做,这似乎可行。首先我将一月的日子添加到字典中,所有的起始值都是0

var sorted = new SortedDictionary<DateTime, int>();

// Add the days of January to our sorted dictionary
var startDate = new DateTime(2017, 1, 1);
var endDate = new DateTime(2017, 2, 1);

for (var date = startDate; date < endDate; date = date.AddDays(1))
{
    sorted.Add(date.Date, 0); // Just add the .Date part to be safe
}

接下来,我创建了一个包含 100 个提交的列表,每个提交都在 1 月的随机一天(因此最终平均每天应该有 3.2 个提交:

var commits = new List<CommitInfo>();
var rnd = new Random();

for (int i = 0; i < 100; i++)
{
    commits.Add(new CommitInfo
    {
        TimeStamp = startDate.AddDays(rnd.Next(0, 31)) 
    });
}

现在,要使用这些提交更新我们的字典,我们可以遍历每个提交,查看字典是否包含键,如果包含,则更新该项的值:

// Update our dictionary with the commits 
foreach (var commit in commits)
{
    if (sorted.ContainsKey(commit.TimeStamp.Date))  // Just compare the date part
    {
        sorted[commit.TimeStamp.Date] += 1;
    }
}

现在您可以像以前一样从字典中获取值:

// Populate our values array
int[] values = sorted.Values.ToArray();

这是字典的最终结果,因此您可以看到结果已更新,以及 Values 的总和,因此您可以看到添加了所有 100 个提交:

// Display new dictionary results
Console.WriteLine("\nDictionary Contents After Processing Commits");
Console.WriteLine("--------------------------------------------");
foreach (var dictionaryItem in sorted)
{
    Console.WriteLine("Key: {0} Value: {1}", dictionaryItem.Key.ToString().PadRight(27), 
        dictionaryItem.Value);
}

Console.WriteLine($"\nSum of all values is: {sorted.Values.Sum()}\n");

【讨论】:

    【解决方案2】:

    我使用了一个 do-while 循环,并将“日期”用作班级内部的私有文件。如果我现在检查两个日期,它将增加。

            date = assignment.Start.Date;
            do
            {
    
                foreach (CommitInfo dates in commits)
                {
    
                    if (dates.TimeStamp.Date == date.Date)
                    {
                        Sorted[date.Date] += 1;
                    }
    
                }
                date = date.AddDays(1).Date;
            } while (date.Date < assignment.End.Date);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-15
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      相关资源
      最近更新 更多