I need a Linq To SQL Query that Counts the rows of a table that fall within time brackets of an arbitrary interval. The Table has a single DateTime field.
Google got me to this SQL (http://ebersys.blogspot.ca/2010/12/sql-group-datetime-by-arbitrary-time.html)
declare @interval int
set @interval = 5
select convert(varchar(8), DTColumn, 1)+' '
+convert(varchar(2), datepart(hh, DTColumn))+':'
+convert(varchar(2), datepart(mi, DTColumn)/@interval*@interval)
, count(*)
from the_table
group by convert(varchar(8), DTColumn, 1)+' '
+convert(varchar(2), datepart(hh, DTColumn))+':'
+convert(varchar(2), datepart(mi, DTColumn)/@interval*@interval)
I could move the query to SQL but prefer to keep the DAL consistent LinqToSQL. The best I could come up with is this, but errors with "Tick is not supported in SQL"
int interval = Convert.ToInt32(uxTxtInterval.Text);
var q = (from d in dc.theTable.OrderBy(o => o.dmMe).ThenBy(o => o.dmWith).ThenBy(o => o.dmCreatedAt)
group d by
(
Math.Floor(new TimeSpan(d.dmCreatedAt.Ticks).TotalMinutes / interval)
)
into grpTable
select
new
{
time = grpTable.Key, // ?? needs to be the DateTime not ticks
count = grpTable.Count()
}
);
Any help greatly appreciated :-)
Thanks!