按周分组和平均时间跨度

Group And Average Timespans By Week

我有许多具有不同日期时间的记录,但我需要在图形布局中跟踪这些记录。我目前已经设法检索每条记录的时间跨度

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
}

这当然会 return 给我一个时间跨度列表,但它本身并不是很有帮助。

如何更好地处理这些时间跨度的每周(或每月)平均值列表。

我考虑的问题是时间跨度忘记了它们的起点和终点,因此虽然可以对这些点进行平均,但如何在分组方法中进行平均?

输出;一组 DateTimes 的平均时间跨度将挂接到图表中以进行可视化显示。

这可能不是可用的最佳解决方案,并且逻辑上存在一些明显的漏洞,但是目前这是一个在需要的范围内工作的解决方案(低优先级系统因此高精度不是需要)。 另外,对于其中所有经过审查的变量,例如 class 名称等,我深表歉意,我理解阅读起来更痛苦。

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