public async Task Import() { _logger.LogInformation("Import starting"); var timeStarted = DateTime.UtcNow; var downloadName = _nationalStatisticsLocationService.GetName(); var previousImport = await _auditRepository.GetLastImportByType(ImportType.OnsLocation); if (previousImport?.Name != null && previousImport.Name.Equals(downloadName, StringComparison.CurrentCultureIgnoreCase)) { _logger.LogWarning("No new items to import"); return; } var items = (await _nationalStatisticsLocationService.GetLocations()).ToList(); if (!items.Any()) { _logger.LogWarning("No items to import"); return; } _logger.LogInformation("Deleting from Import table"); _importRepository.DeleteAll(); _logger.LogInformation("Inserting into Import table"); var featureItems = items .ToList() .Where(item => !string.IsNullOrEmpty(item.Attributes.LocationName)) .Where(item => !string.IsNullOrEmpty(item.Attributes.LocalAuthorityName)) .Where(item => !string.IsNullOrEmpty(item.Attributes.LocationAuthorityDistrict)) .Where(item => item.Attributes.PlaceName != PlaceNameDescription.None).ToList() .GroupBy(c => new { c.Attributes.LocalAuthorityName, c.Attributes.LocationName, c.Attributes.LocationAuthorityDistrict }) .Select(item => item.First()) .GroupBy(c => new { c.Attributes.Id }) .Select(SelectDuplicateByLocalAuthorityDistrictDescription) .ToList(); await _importRepository.InsertMany(featureItems.Select(c => (Domain.Entities.LocationImport)c.Attributes).ToList()); var importedItems = (await _importRepository.GetAll()).ToList(); _logger.LogInformation("Deleting from main table"); _repository.DeleteAll(); _logger.LogInformation("Inserting into main table"); await _repository.InsertMany(importedItems.Select(c => (Domain.Entities.Location)c).ToList()); await _auditRepository.Insert(new ImportAudit(timeStarted, importedItems.Count, downloadName)); }