LINQ的
from c in customerReadings
group c by new { date = GetGroupingDateKey(DateRangeType, c.ReadDate), name = c.Name } into g
select new MeterReadingsForChart
{
ReadDate = g.Key.date,
Name = g.Key.name,
Value = g.Sum(y => y.Value),
TimeInterval = DateRangeType
};
GetGroupingDateKey()
private static DateTime GetGroupingDateKey(MeterReadingsTimeIntervals DateRangeType, DateTime Date)
{
DateTime date = new DateTime();
switch (DateRangeType)
{
case MeterReadingsTimeIntervals.Hourly:
//For Example data is betweet 10:05 - 11:05
//DateTime offSet = Date.AddMinutes(-5);
//date = new DateTime(offSet.Year, offSet.Month, offSet.Day, offSet.Hour, 5, 0);
date = new DateTime(Date.Year, Date.Month, Date.Day, Date.Hour, 0, 0);
break;
case MeterReadingsTimeIntervals.Daily:
date = new DateTime(Date.Year, Date.Month, Date.Day, 0, 0, 0);
break;
case MeterReadingsTimeIntervals.Weekly:
date = Date.AddDays(-((7 + Date.DayOfWeek - DayOfWeek.Monday) % 7));
break;
case MeterReadingsTimeIntervals.Monthly:
date = new DateTime(Date.Year, Date.Month, 1, 0, 0, 0);
break;
case MeterReadingsTimeIntervals.Yearly:
date = new DateTime(Date.Year, 1, 1, 0, 0, 0);
break;
}
return date;
}
记录时间:10/10/2012 - 09:15至10:15之间
Hourly groupped data count : 2 (expected)
Daily groupped data count : 1 (expected)
Weekly groupped data count : 5 (group count (not expected))
Monthly groupped data count : 1 (expected)
Yearly groupped data count : 1 (expexted)
如何按周分组记录。我的每周分组有什么不对?
提前致谢。
答案 0 :(得分:3)
我认为你的Date
有时间部分 - 你需要把它归零,例如。
date = Date.Date.AddDays(-((7 + Date.DayOfWeek - DayOfWeek.Monday) % 7));