将已排序字典的 DateTime 与 DateTime 字段进行比较

compare DateTime of sorted dictionary with DateTime field

我有一个需要用排序字典初始化的数组。该数组将包含整数值,这些值已在字典中进行了修改。该数组的目的是包含在特定日期完成的提交。使用排序后的字典,我将 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”填充排序字典(在 class 构造函数中)。

        //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 class 中获得了一些提交。这些提交包含一个字段 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' 数组。这是通过以下代码完成的:

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

有人可以帮助我如何让它正常工作吗?因为我已经尝试了很多东西,但是 none 成功了。

我使用了一个 do-while 循环并将 'date' 用作我的 class 中的私有文件。如果我现在检查两个日期,它将增加。

        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);

我在您的原始代码中看到的唯一一件事是,当您循环提交时,对于每个提交,您还循环了字典键,这是不必要的。您需要做的就是,对于每个提交,查看是否 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();

这是字典的最终结果,因此您可以看到结果已更新,以及值的总和,因此您可以看到添加了所有 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");