.
Get the date of the first day of the week. you can use this code:
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
}
Then you can group by the first date of the week.
So this code in plain SQL:
SELECT * From T GROUP BY DATEPART(wk, T.Date)
can run in linq to sql like this
T.GroupBy(i => i.Date.StartOfWeek(DayOfWeek.Monday));
source
share