我创建了一个ASP.NET MVC项目,并有一个问题。我编写了此函数以从数据库中返回值:
public List<List<ChartDatanDTO>> GetDataForChart(string function)
{
List<ChartDatanDTO> allItems = db.ChartDatas
.Where(x => x.Function == function)
.Select(x => new ChartDatanDTO
{
durat = x.Duration,
err = x.IsError,
time = x.Timestamp
})
.ToList();
List<List<ChartDatanDTO>> sublist = new List<List<ChartDatanDTO>>();
sublist = allItems.GroupBy(x => new
{
x.time.Date,
x.time.Hour
})
.Select(x => x.ToList()).ToList();
return sublist;
}
此方法在我的浏览器中返回此值:
(2) […]
0: (2) […]
0: Object { duration: 3, err: false, time: "2019-06-04T09:16:21.15" }
1: Object { duration: 2, err: false, time: "2019-06-04T09:16:22.427" }
1: (4) […]
0: Object { duration: 3, err: false, time: "2019-06-04T10:16:31.16" }
1: Object { duration: 2, err: false, time: "2019-06-04T10:16:32.43" }
2: Object { duration: 5, err: false, time: "2019-06-04T10:16:41.177" }
3: Object { duration: 2, err: false, time: "2019-06-04T10:16:42.44" }
现在,我想扩展我的GetDataForChart方法,该方法将创建两个数组,这些数组的持续时间平均值和小时的值。
它应该像这样:
一个平均持续时间(每小时)的数组:[2.5, 3]
一个小时的数组:[09:00, 10:00]