Say I have some class like this:
public class MyClass
{
public DateTime Date { get; set; }
public string Code { get; set; }
}
And some values like:
| Date | Code |
|------------ |------ |
| 03/04/2017 | 1234 |
| 31/03/2017 | 1234 |
| 29/03/2017 | 1234 |
| 29/03/2017 | 4321 |
| 25/03/2017 | 4321 |
| ... | ... |
I want to group these by the Code
field AND also group by the Date however I want the Date to be grouped by a range. The calculation on this will find the date at the start of the week (Monday of that week) and also the end of the week (Friday of that week) and then give me some results like:
| 03/04/2017 | 1234 | < week beginning 03/04 and code=1234
| 31/03/2017 | 1234 | < week beginning 27/03 and code=1234
| 29/03/2017 | 1234 |
| 29/03/2017 | 4321 | < week beginning 27/03 and code=4321
| 25/03/2017 | 4321 | < week beginning 20/03 and code=4321
I've tried doing a range but I think what I'm doing is pretty useless (StartOfWeek
is an extension method from here):
data.Where(d => d.Date >= d.Date.StartOfWeek(DayOfWeek.Monday) && d.Date <= d.Date.StartOfWeek(DayOfWeek.Monday).AddDays(6));
Note: I'm using Entity Framework for my actual project but when I'm doing this I am fetching all the data from the MyClass
table and I want to do all the above to filter and group the data appropriately.