I have a collection of DateTime objects. I need to parse subcollections from this source list in order to capture related items based on repetition. Therefore, I need to take this single original collection:
var collectionOfDateTime = GetDateTimeCollection();
and translate this into a list of DateTime collections, where each collection includes a set of dates from the first list that follows a specific recurrence pattern.
In my examples below, I do not include time, but in real demand, these elements have Date and Time elements for them. So, for example, the dates should be 7 days apart, but also the same (one time February 3 at 11 am does not correspond to February 10 at 3 pm, but it corresponds to February 10 at 11 am)
For example, suppose my recurrence pattern is “Weekly” (in other cases, it can be “Monthly”), and my collection of dates looks like this:
var date1 = DateTime.Today.AddHours(8); var date2 = DateTime.Today.AddWeeks(1).AddHours(8); var date3 = DateTime.Today.AddDays(3); var date4 = DateTime.Today.AddWeeks(8).AddHours(6); var collectionOfDateTime = new List<DateTime>() { date1, date2, date3, date4 };
I need a function (allowing it to StripOutSubCollections() ) to pass to collectionOfDateTime and the fact that it is "Weekly" and return a single collection that includes date1 , date2 (since they are all part of the same weekly slot). Note date3 is not suitable, and date4 is also not suitable, since the clock does not match with the others
As another example, to help prove this requirement, if the entrance to the original collection was as follows:
var date1 = DateTime.Today; var date2 = DateTime.Today.AddWeeks(1); var date3 = DateTime.Today.AddDays(3); var date4 = DateTime.Today.AddWeeks(8); var date5 = DateTime.Today.AddDays(3).AddWeeks(2); var collectionOfDateTime = new List<DateTime>() { date1, date2, date3, date4, date5 };
I would like this function to return 2 lists (one list with date1 , date2 and date4 ) and another list with date3 and date5 .
Let me know if I need additional examples to formulate requirements? Please note that it is possible that one of the dates may fall into several output lists, which is fine.
I can convert weekly to the number 7 and loop through each element, from the first element to the end, then the second element to the end, then the third element, etc., but I would like to see if there was another elegant way