public List <AvailableOffSitePeriods> GetAllAvailableOffSitePeriods() { List <AvailableOffSitePeriods> AOSP = new List <AvailableOffSitePeriods>(); DateTime FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET = CourseSearchPeriodStartDate.Date; while (FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.Date < CourseSearchPeriodEndDate.Date) { // Ensure that this course can start on this day... eg Monday through Thursday while (FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.Date < CourseSearchPeriodEndDate.Date) { Boolean IsFirstDayFound = false; while (!IsFirstDayFound) { EnumDayOfWeeks DayOfTheWeek = CustomDateTime.CustomDayOfTheWeek(FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.DayOfWeek); if ((from a in SetOfDaysOfWeekCourseCanBeScheulded where a == DayOfTheWeek//a.HasFlag(DayOfTheWeek) select a).ToList <EnumDayOfWeeks>().Count() > 0) { IsFirstDayFound = true; } else { FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET = FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.AddDays(1); } } //Inclusive of the start date ( that why subtrtact day from the start date. DateTime EndDateForCurrentPossibleSchedule = CustomDateTime.getCustomDateTime(FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.Date.AddDays(-1), CourseDuration, SetOfDaysOfWeekCourseCanBeScheulded); ///get all possible cominations for the period foreach (FacilitatorPeriod FP in FacilitatorAvailablePeriods.OrderBy(a => a.StartDate)) { //Deterine if the Period is greater than the course period(Course Duration) else cant schedule in this period. if (FP.GetAmountOfDaysInPeriod() >= CourseDuration) { //Determine if the facilitator could fall into this course period if (FP.StartDate.Date <= FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.Date && EndDateForCurrentPossibleSchedule.Date <= FP.EndDate.Date) { // this period can fit into this course period. //List<OnSiteVenuePeriod> OnSiteVenueAvailablePeriods foreach (OffSiteVenuePeriod OSVAP in OffSiteVenueAvailablePeriods) { //Deterine if the Period is greater than the course period(Course Duration) else cant schedule in this period. if (OSVAP.GetAmountOfDaysInPeriod() >= CourseDuration) { if (OSVAP.StartDate <= FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.Date && EndDateForCurrentPossibleSchedule.Date <= OSVAP.EndDate.Date) { AOSP.Add(new AvailableOffSitePeriods() { CourseID = base.CourseID, CourseName = base.CourseName, CourseStartDate = FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.Date, CourseEndDate = EndDateForCurrentPossibleSchedule.Date, FacilitatorID = FP.FacillitatorID, FacilitatorName = FP.FacilitatorName, VenueID = OSVAP.VenueID, VenueName = OSVAP.VenueName, VenueAssociatedCompany = OSVAP.VenueAssociatedCompany }); } } } } } } FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET = FIRST_DAY_CAN_BE_SCHEDULED_FROM_SET.AddDays(1); } } return(AOSP); }