public List <AsCustomerHistory> GetReferralCustomerHistoryUsingAsCustomerId(int asCustomerId) { var myCustomerHistory = new List <AsCustomerHistory>(); try { // Get results from AS database. myCustomerHistory = _myIApplicationsRepository.AsCustomerHistoryRepository.GetFilteredToList(x => x.customerID == asCustomerId); //Calculate the corresponding AP customerID var latestAsReferralId = _myIApplicationsRepository.ReferralsServiceRepository .GetFilteredToIQuerable(x => x.CustomerId == asCustomerId).OrderByDescending(x => x.ReferralId) .FirstOrDefault()?.ReferralId; var apCustomerId = _myIApplicationsRepository.LegalReferredInOutCustomersLegalRepository .GetFilteredToIQuerable(x => x.ReferralId == latestAsReferralId).FirstOrDefault()?.CustomerId; //Add APCustomerHistory to list and order. myCustomerHistory.AddRange(_myIApplicationsRepository.LegalCustomerHistoryRepository .GetFilteredToList(x => x.customerID == apCustomerId).Select(apCustomerHistoryListItem => _myIAutoMapperService.MapAsCustomerHistory(apCustomerHistoryListItem)).ToList()); myCustomerHistory = myCustomerHistory.OrderByDescending(x => x.logtime).ToList(); } catch (Exception e) { _myInLogService.LogError(e, "Method: Querying.Referrals.Services.Applications.Services.AppRepositoryServices.ReferralsService/LegalRepositoryService/" + nameof(GetReferralCustomerHistoryUsingAsCustomerId) + ": " + e.Message); } return(myCustomerHistory); }