private List <MarketRouteSync> MapRouteSchedulesToRouteSync(IOrderedQueryable <MarketRouteSchedule> marketSchedules, DateTime?requestDate) { var results = new List <MarketRouteSync>(); marketSchedules.ForEach(m => { var route = Mapper.Map <MarketRoute, MarketRouteSync>(m.MarketRoute); route.RouteScheduleId = m.MarketRouteScheduleId; route.Market = m.MarketRoute.MarketRouteMap.OrderBy(s => s.SortOrder).Select(x => new MarketSync() { MarketName = x.Market.Name, MarketId = x.MarketId, SortOrder = x.SortOrder, Description = x.Market.Description, MarketCustomers = (from s in x.Market.MarketCustomers join b in _currentDbContext.Account on s.AccountId equals b.AccountID join mc in x.Market.MarketCustomers on b.AccountID equals mc.AccountId orderby mc.SortOrder where (!mc.SkipFromDate.HasValue && !mc.SkipToDate.HasValue) || (requestDate <= mc.SkipFromDate && requestDate >= mc.SkipToDate) select new MarketCustomersSync() { ContactNumber = b.AccountContacts.Any() ? b.AccountContacts.FirstOrDefault()?.TenantContactPhone : "", MarketCustomerName = b.CompanyName, FullAddress = b.FullAddress, SortOrder = mc.SortOrder, IsSkippable = mc.IsSkippable, MarketCustomerId = mc.Id, MarketCustomerAccountId = b.AccountID }).ToList() }).Where(x => x.MarketCustomers.Any()).ToList(); results.Add(route); }); return(results); }