public async Task <int> Load(Guid extractId, int found, bool diffSupport) { int count = 0; var mapper = diffSupport ? ExtractDiffMapper.Instance : ExtractMapper.Instance; try { DomainEvents.Dispatch( new ExtractActivityNotification(extractId, new DwhProgress( nameof(ContactListingExtract), nameof(ExtractStatus.Loading), found, 0, 0, 0, 0))); StringBuilder query = new StringBuilder(); query.Append($" SELECT s.* FROM {nameof(TempContactListingExtract)}s s"); query.Append($" INNER JOIN PatientExtracts p ON "); query.Append($" s.PatientPK = p.PatientPK AND "); query.Append($" s.SiteCode = p.SiteCode "); const int take = 1000; var eCount = await _tempContactListingExtractRepository.GetCount(query.ToString()); var pageCount = _tempContactListingExtractRepository.PageCount(take, eCount); int page = 1; while (page <= pageCount) { var tempContactListingExtracts = await _tempContactListingExtractRepository.ReadAll(query.ToString(), page, take); var batch = tempContactListingExtracts.ToList(); count += batch.Count; //Auto mapper var extractRecords = mapper.Map <List <TempContactListingExtract>, List <ContactListingExtract> >(batch); foreach (var record in extractRecords) { record.Id = LiveGuid.NewGuid(); } //Batch Insert var inserted = _ContactListingExtractRepository.BatchInsert(extractRecords); if (!inserted) { Log.Error($"Extract {nameof(ContactListingExtract)} not Loaded"); return(0); } Log.Debug("saved batch"); page++; DomainEvents.Dispatch( new ExtractActivityNotification(extractId, new DwhProgress( nameof(ContactListingExtract), nameof(ExtractStatus.Loading), found, count, 0, 0, 0))); } await _mediator.Publish(new DocketExtractLoaded("NDWH", nameof(ContactListingExtract))); return(count); } catch (Exception e) { Log.Error(e, $"Extract {nameof(ContactListingExtract)} not Loaded"); return(0); } }