我在Visual Studios 2017中创建了一个wpf应用程序。我目前正在尝试在有序的日期时间列表中查找最近的日期时间。似乎我的“最近”值没有在循环中更新,但对我来说一切都是正确的。
这是我的代码
while (r < dataTable.Rows.Count) //get nearest value to the current target
{
long nearest = long.MaxValue;
long dtDiff = 0;
while(nearest > dtDiff)
{
DateTime dtCurr = Convert.ToDateTime(dataTable.Rows[r][dtCol].ToString());
dtDiff = Math.Abs(dtTarget.Ticks - dtCurr.Ticks);
if(dtDiff < nearest)
{
Debug.WriteLine("Smaller value found r:" + r);
Debug.WriteLine("diff:" + dtDiff.ToString() + " nearest:" + nearest);
nearest = dtDiff;
r++;
}
else
{
Debug.WriteLine("nearest value found " + r);
long v = long.Parse(dataTable.Rows[r][dtCol + 1].ToString());
values.Add(new NormValue { datetime = dt, value = v});
dtTarget.AddSeconds(inc);
break;
}
}
}在if(dtDiff < nearest)语句中,最近的变量应该被更新,但是根据我的调试打印语句,它永远不会改变。我不确定我做错了什么。
编辑:这是datatable的一个示例:
01-05-16 00:00:10
01-05-16 00:00:40
01-05-16 00:01:10
01-5-16 00:01:40
01-05-16 00:02:10
01-05-16 00:02:40
01-05-16 00:03:10
我的目标日期时间值以增量递增,因此它将如下所示
01-05-16 00:00:10
01-05-16 00:00:20
01-05-16 00:00:30
01-05-16 00:00:40
因此,可以使用相同的值或完全跳过值
https://stackoverflow.com/questions/51830699
复制相似问题