I am trying to do a query to an IEnumerable<Object>
to group by week, for example:
Project(Name, DateStart,ID)
I have IEnumerable<Project>
and I want to do a report, grouping by week.
For exmaple:
Week 1
Project1 8/4/2013 ID1
Project2 9/4/2013 ID2
Week 2
Project1 16/4/2013 ID3
Project2 18/4/2013 ID5
Week 3
Project1 24/4/2013 ID7
Project2 26/4/2013 ID8
Please if someone can give me a hand I really appreciate it! I was trying to do a lambda expression but without success.
Thanks!
I think just a simple projection will suffice:
You can use the
GetWeekOfMonth
extension posted here:and then do something like:
In addition to
Tim Schmelter's
answer you can get the date of the first day in the week then group by that date.To get the date of the first day in the week. you can use this code:
then you can group by the first date of the week like this:
You will need to define a function that can determine which week a date is in:
Then the LINQ query to group is:
You can iterate over the grouped projects list:
I think to simplify this task you can add WeekNumber property into Project class and fill it when you are creating object of Project after you can easy group by WeekNumber property