public async Task <Models.CourtList.CourtList> CourtListAsync(string agencyId, string roomCode, DateTime proceeding, string divisionCode, string fileNumber) { var proceedingDateString = proceeding.ToString("yyyy-MM-dd"); var agencyCode = await _locationService.GetLocationCodeFromId(agencyId); async Task <CourtList> CourtList() => await _filesClient.FilesCourtlistAsync(agencyId, roomCode, proceedingDateString, divisionCode, fileNumber); async Task <CourtCalendarDetailByDay> CourtCalendarDetailByDay() => await _filesClient.FilesCourtCalendarDetailsByDayAsync(_requestAgencyIdentifierId, _requestPartId, agencyCode, proceeding.ToString("yyyy-MM-dd HH:mm:ss.S"), roomCode); var courtCalendarDetailsTask = _cache.GetOrAdd($"CivilCourtCalendarDetails-{agencyId}-{roomCode}-{proceedingDateString}-{fileNumber}", CourtCalendarDetailByDay); var originalCourtListTask = _cache.GetOrAddAsync($"CourtList-{agencyId}-{roomCode}-{proceedingDateString}-{fileNumber}", CourtList); //Query by courtCalendarDetails, because it returns quite a bit faster than the courtList. var courtCalendarDetails = await courtCalendarDetailsTask; var civilCourtCalendarAppearances = courtCalendarDetails?.Appearance .WhereToList(app => app.CourtDivisionCd == CourtCalendarDetailAppearanceCourtDivisionCd.I); var criminalCourtCalendarAppearances = courtCalendarDetails?.Appearance .WhereToList(app => app.CourtDivisionCd == CourtCalendarDetailAppearanceCourtDivisionCd.R); var civilFileIds = civilCourtCalendarAppearances.SelectToList(ccl => ccl.PhysicalFileId); var criminalFileIds = criminalCourtCalendarAppearances.SelectToList(ccl => ccl.MdocJustinNo); if (civilFileIds.Count == 0 && criminalFileIds.Count == 0) { return(new Models.CourtList.CourtList()); } //Start file details tasks. var civilFileDetailTasks = CivilFileDetailTasks(civilFileIds); var criminalFileDetailTasks = CriminalFileDetailTasks(criminalFileIds); //Start appearances tasks. var civilAppearanceTasks = CivilAppearancesTasks(proceeding, civilFileIds); var criminalAppearanceTasks = CriminalAppearancesTasks(proceeding, criminalFileIds); //Start file content tasks. var civilFileContentTasks = CivilFileContentTasks(civilFileIds); //Await our asynchronous requests. var civilFileDetails = (await civilFileDetailTasks.WhenAll()).ToList(); var criminalFileDetails = (await criminalFileDetailTasks.WhenAll()).ToList(); var civilAppearances = (await civilAppearanceTasks.WhenAll()).ToList(); var criminalAppearances = (await criminalAppearanceTasks.WhenAll()).ToList(); var civilFileContents = (await civilFileContentTasks.WhenAll()).ToList(); var originalCourtList = await originalCourtListTask; var courtList = _mapper.Map <Models.CourtList.CourtList>(originalCourtList); courtList.CivilCourtList = await PopulateCivilCourtListFromFileDetails(courtList.CivilCourtList, civilFileDetails); courtList.CriminalCourtList = await PopulateCriminalCourtListFromFileDetails(courtList.CriminalCourtList, criminalFileDetails); courtList.CivilCourtList = await PopulateCivilCourtListFromAppearances(courtList.CivilCourtList, civilAppearances); courtList.CriminalCourtList = await PopulateCriminalCourtListFromAppearances(courtList.CriminalCourtList, criminalAppearances); courtList.CivilCourtList = PopulateCivilCourtListFromCourtCalendarDetails(courtList.CivilCourtList, civilCourtCalendarAppearances); courtList.CriminalCourtList = PopulateCriminalCourtListFromCourtCalendarDetails(courtList.CriminalCourtList, criminalCourtCalendarAppearances); courtList.CivilCourtList = PopulateCivilCourtListFromFileContent(courtList.CivilCourtList, civilFileContents); return(courtList); }