林克
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)
如何按周对记录进行分组。我的每周分组有什么问题?
提前致谢。