I need help with a project I am working on, I import data form a CSV, I already can import the csv in a database and upload it in a database.
CSV example :
Id;ProductCode;SerialNumberStart;Quantity;IsDistributed;DateCreated
0;BTT 1;50000;50;false;2025-01-16
Now I have a database called PRODUCT
where I have a field called CODE
where I have the list of Product code like
"BTT 1","BTT 2", etc...,
now I need to verify that ALL items in the csv list exist in the db list
or in other words
there is no item in csv list that does not exist in db list.(my supervisor)
//StockBatchItemDto is the DB where I upload the csv
records = csvReader.GetRecords<StockBatchItemDto>().ToList();
var productCodes = records
.Select(record => record.ProductCode)
.Distinct()
.ToList();
var matchProducts = await _dbContext.Products
.Where(prod => productCodes.Contains(prod.Code))
.Select(prod => prod.Code).ToListAsync();
now how I can do that? and can someone explain it to me please.
If the unique set of codes in the DB is fairly small you can do:
var dbCodes = _dbContext.Products.Select(p => p.Code).Distinct();
var csvCodes = csvReader.GetRecords<StockBatchItemDto>()
.Select(record => record.ProductCode);
var missingCodes = csvCodes.Except(dbCodes).ToList();
if (!missingCodes.Any())
{
Console.WriteLine("All items in the CSV list exist in the DB list.");
}