I'm looking for the most efficient way to suck out a series of monthly counts of records in my database, but adjusting for time zone, since the times are actually stored as UTC. I would like my result set to be a series of objects that include month, year and count.
I have LINQ to SQL objects that looks something like this:
public class MyRecord {
public int ID { get; set; }
public DateTime TimeStamp { get; set; }
public string Data { get; set; }
}
I'm not opposed to using straight SQL, but LINQ to SQL would at least keep the code a lot more clean. The time zone adjustment is available as an integer (-5, for example). Again, the result set what I'm looking for is objects containing the month, year and count, all integers.
Any suggestions? I can think of several ways to do it straight, but not with a time zone adjustment.
EDIT: The answer below got me headed in the right direction. This is what I ultimately ended up with:
var counts = _context.MyRecord
.Select(r => new {original = r.TimeStamp, adjusted = TimeAdjust.GetAdjustedTime(Config.TimeZoneAdjustment, r.TimeStamp)}).ToArray()
.GroupBy(r => new {r.adjusted.Month, r.adjusted.Year})
.Select(g => new MonthCount { Count = g.Count(), Year = g.Key.Year, Month = g.Key.Month })
.OrderByDescending(g => g.Year).ThenByDescending(g => g.Month);
Basically I'm pulling all of the date down, which works OK given the limited scope of this app. The TimeAdjust function gets "real" adjusted times, accounting for DLS. The ToArray() call is made to avoid the lazy execution that pisses of SQL because of the time adjustment function.