CreationTime是DateTime类型
group by 年/月/日/小时
group by 年
(from d in YourData.OrderBy(x => x.CreationTime) group d by new { time = new { d.CreationTime.Year } } into g select new { AverageValue = g.Average(p => p.Value), CreationTimeStr = g.Key.time.Year, MaxValue = g.Max(x => x.Value), MinValue = g.Min(x => x.Value) }).ToList();
group by 小时
(from d in YourData.OrderBy(x => x.CreationTime) group d by new { time = new { d.CreationTime.Year, d.CreationTime.Month, d.CreationTime.Day,d.CreationTime.Hour } } into g select new { AverageValue = g.Average(p => p.Value), CreationTimeStr = g.Key.time.Year + "-" + g.Key.time.Month + "-" + g.Key.time.Day+" "+g.Key.time.Hour+":00:00", MaxValue = g.Max(x => x.Value), MinValue = g.Min(x => x.Value) }).ToList();
其他类推
group by 半小时
group by 半小时=group by 30分钟
(from d in YourData.OrderBy(x => x.CreationTime) group d by new { time = new { d.CreationTime.Year, d.CreationTime.Month, d.CreationTime.Day, d.CreationTime.Hour, Minute = (d.CreationTime.Minute / 30) * 30 } } into g select new { Value = g.Average(p => p.Value), CreationTimeStr = g.Key.time.Year + "-" + g.Key.time.Month + "-" + g.Key.time.Day + " " + g.Key.time.Hour + ":"+g.Key.time.Minute.ToString().PadRight(2,'0')+ ":00", MaxValue = g.Max(x => x.Value), MinValue = g.Min(x => x.Value) }).ToList();
参考资料:https://stackoverflow.com/questions/12060743/grouping-by-every-n-minutes