我有许多具有各种DateTimes的记录,但我需要在图形布局中跟踪这些记录。我目前已设法检索每条记录的时间跨度
List<myclass> recs = context.myclass.Where(c => c.RequestedTimestamp >= start & c.DeliveredTimestamp <= end).ToList();
foreach (var item in recs)
{
TimeSpan diff = (TimeSpan)(item.DeliveredTimestamp - item.RequestedTimestamp);
//more to come, this is where I realized I have issues
}
这当然会返回一个时间表列表,但这本身并不是很有帮助。
为了获得这些时间跨度的每周(或每月)平均值列表,如何更好地接近这一点。
我考虑的问题是时间跨度会忘记它们的起点和终点,因此虽然可以对这些点进行平均,但是如何以分组方法执行平均值?
输出;一组DateTimes的平均时间跨度将被挂钩到图表中以进行可视化显示。
答案 0 :(得分:0)
这可能不是最好的解决方案,并且逻辑中存在一些明确的漏洞,但是现在这是一个在需要的范围内工作的解决方案(低优先级系统因此不需要高精度) 。 另外,对于那里的所有审查变量,例如班级名称等,我很难过,我理解它更难以阅读。
public static List<ChartPoint> RenderWeeklyAverageChart(List<myclass> myclass, DateTime start, DateTime end)
{
DateTime datePointer = start; //1st date in list
List<ChartPoint> points = new List<ChartPoint>();
double average = 0;
if (myclass!= null && myclass.Count > 0)
{
while (datePointer < DateTime.Now)
{
//Filter By Selected Week
List<myclass> Weekmyclass = myclass.Where(c => c.RequestedTimestamp >= datePointer &&
c.RequestedTimestamp < datePointer.AddDays(7)).ToList();
//If there are records, find the average timespan of the records
if (Weekmyclass != null && Weekmyclass.Count > 0)
{
long ticks = (long)WeekSlabs.Average(c => (c.DeliveredTimestamp.Value - c.RequestedTimestamp.Value).Ticks);
average = TimeSpan.FromTicks(ticks).TotalHours;
points.Add(new ChartPoint
{
PointValue = average,
AxisXText = datePointer.ToShortDateString() + " - "
+ datePointer.AddDays(7).ToShortDateString()
});
}
else
{
//Otherwise Add a Zero Record
points.Add(new ChartPoint
{
PointValue = 0,
AxisXText = datePointer.ToShortDateString() + " - "
+ datePointer.AddDays(7).ToShortDateString()
});
}
datePointer = datePointer.AddDays(7);
}
}
return points;
}