public override void FiltrByDate() { if (Resorts != null) { List <HolidayHome> HolidayHomes = Resorts.SelectMany(x => x.HolidayHomes).ToList(); List <HolidayHome> listofHolidayHomes = new List <HolidayHome>(HolidayHomes); List <DateTime> timeinterval = new List <DateTime>(); if (BookFrom == DateTime.MinValue) { BookFrom = DateTime.Now.AddDays(1); } if (BookTo == DateTime.MinValue) { BookTo = BookFrom.AddMonths(1); } int nightsCount = (int)Math.Round((BookTo - BookFrom).TotalDays); for (int i = 0; i < nightsCount; i++) { DateTime dayinter = BookFrom; dayinter = dayinter.AddHours(16); dayinter = dayinter.AddDays(i); timeinterval.Add(dayinter); } foreach (var holidayhome in HolidayHomes) { foreach (var reservetime in holidayhome.reserveTimes) { foreach (var day in timeinterval) { if (day >= reservetime.bookingFrom && day <= reservetime.bookingToo) { listofHolidayHomes.Remove(holidayhome); } } } } List <Resort> listresort2 = listofHolidayHomes.Select(l => l.Resort).ToList(); ///////// List <Room> Rooms = Resorts.SelectMany(h => h.Rooms).ToList(); List <Room> listofRooms = new List <Room>(Rooms); List <DateTime> timeinterval2 = new List <DateTime>(); if (BookFrom == null) { BookFrom = DateTime.Now.AddDays(1); } if (BookTo == null) { BookTo = BookFrom.AddMonths(1); } int nightsCount2 = (int)Math.Round((BookTo - BookFrom).TotalDays); for (int i = 0; i < nightsCount2; i++) { DateTime dayinter = BookFrom; dayinter = dayinter.AddHours(16); dayinter = dayinter.AddDays(i); timeinterval2.Add(dayinter); } foreach (var room in Rooms) { foreach (var reservetime in room.reserveTimes) { foreach (var day in timeinterval2) { if (day >= reservetime.bookingFrom && day <= reservetime.bookingToo) { listofRooms.Remove(room); } } } } Rooms = listofRooms; List <Resort> listofResorts = Rooms.Select(x => x.Resort).ToList(); Resorts = new List <Resort>(); Resorts.AddRange(listofResorts); Resorts.AddRange(listresort2); Resorts = Resorts.AsEnumerable <Resort>().Distinct().ToList(); if (Rooms != null && Rooms.Count > 0) { foreach (var resort in Resorts) { List <Room> listDictionary = new List <Room>(); foreach (var room in listofRooms) { if (resort.Rooms.Contains(room)) { listDictionary.Add(room); } } ResortListOfRooms.Add(resort, listDictionary); listDictionary = new List <Room>(); } } if (HolidayHomes != null && HolidayHomes.Count > 0) { foreach (var resort in Resorts) { List <HolidayHome> listDictionary = new List <HolidayHome>(); foreach (var holidayhome in listofHolidayHomes) { if (resort.HolidayHomes.Contains(holidayhome)) { listDictionary.Add(holidayhome); } } ResortListOfHolidayHomes.Add(resort, listDictionary); } } ///////// } if (Hotels != null) { List <Room> Rooms = Hotels.SelectMany(h => h.Rooms).ToList(); List <Room> listofRooms = new List <Room>(Rooms); List <DateTime> timeinterval = new List <DateTime>(); if (BookFrom == null) { BookFrom = DateTime.Now.AddDays(1); } if (BookTo == null) { BookTo = BookFrom.AddMonths(1); } int nightsCount = (int)Math.Round((BookTo - BookFrom).TotalDays); for (int i = 0; i < nightsCount; i++) { DateTime dayinter = BookFrom; dayinter = dayinter.AddHours(16); dayinter = dayinter.AddDays(i); timeinterval.Add(dayinter); } foreach (var room in Rooms) { foreach (var reservetime in room.reserveTimes) { foreach (var day in timeinterval) { if (day >= reservetime.bookingFrom && day <= reservetime.bookingToo) { listofRooms.Remove(room); } } } } Rooms = listofRooms; Hotels = Hotels.AsEnumerable <Hotel>().Distinct().ToList(); if (Rooms != null && Rooms.Count > 0) { foreach (var hotel in Hotels) { List <Room> listDictionary = new List <Room>(); foreach (var room in Rooms) { if (hotel.Rooms.Contains(room)) { listDictionary.Add(room); } } HotelListOfRooms.Add(hotel, listDictionary); } } } }