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;
}
記録時間:2012年10月10日-9時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)
レコードを週ごとにグループ化するにはどうすればよいですか。毎週のグループ化の何が問題になっていますか?
前もって感謝します。