private void SetBookedDatesForFacilitators()
 {
     using (var Dbconnection = new MCDEntities())
     {
         foreach (Facilitator AvailableFacilitaor in GetAllFacilitatorsForSelectedCourse())
         {
             var SetOfFacilitatorSchedules = (from a in Dbconnection.Schedules
                                              where a.ScheduleStartDate >= InitalDate.Date &&
                                              a.FacilitatorID == AvailableFacilitaor.FacilitatorID
                                              select new
             {
                 FacilitatorID = a.FacilitatorID,
                 ScheduleStartDate = a.ScheduleStartDate,
                 ScheduleCompletionDate = a.ScheduleCompletionDate
             }).OrderBy(a => a.ScheduleStartDate).Distinct().ToList();
             foreach (var FaciliatorBookedDates in SetOfFacilitatorSchedules)
             {
                 FacilitatorBookedDateSet FBDS = new FacilitatorBookedDateSet(
                     FacilitatorID: FaciliatorBookedDates.FacilitatorID,
                     StartDate: FaciliatorBookedDates.ScheduleStartDate,
                     EndDate: FaciliatorBookedDates.ScheduleCompletionDate);
                 //add To List
                 FacilitatorBookedDateSets.Add(FBDS);
             }
         }
     };
 }
        public void SetAvailableDatesForFacilitators()
        {
            DateTime SearchEndDate;

            SearchEndDate = this.InitalDate.Date.AddMonths((int)this.AmountOfSearchTimeAhead);

            //For EAch Facilitator
            foreach (int FacilitatorID in (from a in FacilitatorBookedDateSets
                                           select a.FacilitatorID).Distinct <int>().ToList <int>())
            {
                //Steps through their Set of Booked Dates to Determine Available DateSets
                List <FacilitatorBookedDateSet> FBDS = (from a in FacilitatorBookedDateSets
                                                        where a.FacilitatorID == FacilitatorID
                                                        orderby a.StartDate
                                                        select a).ToList <FacilitatorBookedDateSet>();
                // {
                DateTime INITIAL_DATE = this.InitalDate.Date;
                DateTime CURRENT_SEARCH_INDEX_DATE = this.InitalDate.Date;

                //Initial Check To Catch the first Day - IF It Booked
                FacilitatorBookedDateSet FBDS_RETURNED_AS_FOUND = IsDateInAnySet(SetsOfBookedDatesPreFacilitator: FBDS, DateIndex: CURRENT_SEARCH_INDEX_DATE);
                //If Null No DataSet Found.
                while (FBDS_RETURNED_AS_FOUND != null)
                {
                    //Date Index Is Currently In With In A Booked Set.
                    FacilitatorAvailableDateSets.Add(new FacilitatorAvailableDateSet(FacilitatorID, INITIAL_DATE.Date, FBDS_RETURNED_AS_FOUND.StartDate.Date));
                    INITIAL_DATE = FBDS_RETURNED_AS_FOUND.EndDate.Date.AddDays(1);
                    CURRENT_SEARCH_INDEX_DATE = FBDS_RETURNED_AS_FOUND.EndDate.Date.AddDays(1);
                }


                //Checks Each Day Ahead to See If It Falls Into A Booked Time Period.
                while (CURRENT_SEARCH_INDEX_DATE < SearchEndDate)
                {
                    //Check if the Current Start Date Is In Any Of the Booked Date Sets
                    FBDS_RETURNED_AS_FOUND = IsDateInAnySet(SetsOfBookedDatesPreFacilitator: FBDS, DateIndex: CURRENT_SEARCH_INDEX_DATE.Date.AddDays(1));
                    //If Null No DataSet Found.
                    if (FBDS_RETURNED_AS_FOUND == null)
                    {
                        //Current DateIndex Not In Any Booked Date Sets
                        CURRENT_SEARCH_INDEX_DATE = CURRENT_SEARCH_INDEX_DATE.AddDays(1);
                    }
                    else
                    {
                        //Date Index Is Currently In With In A Booked Set.
                        FacilitatorAvailableDateSets.Add(new FacilitatorAvailableDateSet(FacilitatorID, INITIAL_DATE.Date, FBDS_RETURNED_AS_FOUND.StartDate.AddDays(-1)));
                        INITIAL_DATE = FBDS_RETURNED_AS_FOUND.EndDate.Date.AddDays(1);
                        CURRENT_SEARCH_INDEX_DATE = FBDS_RETURNED_AS_FOUND.EndDate.Date;
                        // CURRENT_SEARCH_INDEX_DATE = CURRENT_SEARCH_INDEX_DATE.AddDays(1);
                    }
                }
                FBDS_RETURNED_AS_FOUND = IsDateInAnySet(SetsOfBookedDatesPreFacilitator: FBDS, DateIndex: CURRENT_SEARCH_INDEX_DATE);
                //Capture the Last Possiable Dte Set Avaiable
                if (FBDS_RETURNED_AS_FOUND == null)
                {
                    //Date Index Is Currently In With In A Booked Set.
                    FacilitatorAvailableDateSets.Add(new FacilitatorAvailableDateSet(FacilitatorID, INITIAL_DATE.Date, CURRENT_SEARCH_INDEX_DATE.Date));
                }
            }
        }
        private FacilitatorBookedDateSet IsDateInAnySet(List <FacilitatorBookedDateSet> SetsOfBookedDatesPerFacilitator, DateTime DateIndex)
        {
            FacilitatorBookedDateSet Rtn = null;

            foreach (FacilitatorBookedDateSet FBDS in SetsOfBookedDatesPerFacilitator)
            {
                if ((FBDS.StartDate.Date <= DateIndex.Date) && (FBDS.EndDate.Date >= DateIndex))
                {
                    return(FBDS);
                }
            }

            return(Rtn);
        }