I am importing an old database into a new one. The new database requires each record to have a type when imported. The first record does not have a type, therefore I would like to discard it from the import. I am unsure how to write this in C#
At the moment I have
`public async Task RunImport()
` {
_logger.LogInformation(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss") " | BATHWORKS IMPORT : Start Import");
//BATHWORKS COMPONENT
string type = null;
IEnumerable<BathworksItem> bathworksItem = await GetBathworksItem();
IEnumerable<BathworksItemDto> BathworksItem = await GetBathworksPortalItems();
IEnumerable<BathworksItem> missingBathworksPortalItems = bathworksItems.Where(x => !bathworksportalItem.Any(y => y.Serial == x.Name));
foreach (BathworksItem missingBathworksPortalItem in bathworksItems)
if (!missingBathworksPortalItem.IsNullOrEmpty(type))
{
BathworksItemDto bathworksItemDto = new BathworksItemDto();
bathworksItemDto.ItemNumber = missingBathworksPortalItem.ItemNumber;
bathworksItemDto.Stock = missingBathworksPortalItem.Stock;
bathworksItemDto.Availbility = missingBathworksPortalItem.Availbility;
bathworksItemDto.Company = missingBathworksPortalItem.Company;
bathworksItemDto.Lastupdate = missingBathworksPortalItem.Lastupdate;
bathworksItemDto.Type = missingBathworksPortalItem.Type;
bathworksItemDto.Name = missingBathworksPortalItem.Name;
await _bathworksItem.InsertItem(bathworksItemDto);
}`
I have tried the above and it does not quite do what I was expecting as it does not discard the rogue record, instead I recieve a error 500.
CodePudding user response:
Please check below
public async Task RunImport()
{
_logger.LogInformation(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss") " | BATHWORKS IMPORT : Start Import");
//BATHWORKS COMPONENT
string type = null;
IEnumerable<BathworksItem> bathworksItem = await GetBathworksItem();
IEnumerable<BathworksItemDto> BathworksItem = await GetBathworksPortalItems();
IEnumerable<BathworksItem> missingBathworksPortalItems = bathworksItems.Where(x => !bathworksportalItem.Any(y => y.Serial == x.Name));
foreach (BathworksItem missingBathworksPortalItem in bathworksItems)
// i think you need a nullable check comment down if you need something else
if (missingBathworksPortalItem != null && !string.IsNullOrEmpty(missingBathworksPortalItem.Type))
{
BathworksItemDto bathworksItemDto = new BathworksItemDto();
bathworksItemDto.ItemNumber = missingBathworksPortalItem.ItemNumber;
bathworksItemDto.Stock = missingBathworksPortalItem.Stock;
bathworksItemDto.Availbility = missingBathworksPortalItem.Availbility;
bathworksItemDto.Company = missingBathworksPortalItem.Company;
bathworksItemDto.Lastupdate = missingBathworksPortalItem.Lastupdate;
bathworksItemDto.Type = missingBathworksPortalItem.Type;
bathworksItemDto.Name = missingBathworksPortalItem.Name;
await _bathworksItem.InsertItem(bathworksItemDto);
}```