I have a
List<Advertisement>
where Advertisement contains
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
What I want to do is group the elements in the List<Advertisement> by three specific time ranges using a GroupBy. The three time ranges are as follows:
x => x.StartDate > DateTime.Now.Subtract(TimeSpan.FromDays(1))x => x.StartDate > DateTime.Now.Subtract(TimeSpan.FromDays(7))x => x.StartDate > DateTime.Now.Subtract(TimeSpan.FromDays(365))
So elements with start date in the last day, elements with start date in the last week and elements with start date in the last year.
The group of elements from the last year should include those elements with a start date in the last week and day. And the group of elements from the last week should include those elements from the last day.
I just cant seem to think how to do this. Cheers.
As you want each group to also include the previous groups contents (eg lastweek includes last day) I’ve constructed a union query
How this query works is it creates 3 sub queries to select the relevant data.
Each query then uses a select operator to select the match group name and the original item projected into an anonymous object. (Basically creates a new object with the groupname as one property and the original Item as another).
I then use union to combine the multiple results together in one big list. (Union has the added property that it strips duplicates, but there shouldn’t be any). Once I have the big list I can then Group by the groupname, the second parameter basically puts the orginal item back in as the group value.