I am importing data from a 3rd party and only require a few of the fields/columns in the CSV.
The file has the header row:
id created_time ad_id ad_name adset_id adset_name campaign_id campaign_name form_id form_name is_organic platform full_name email phone_number date_of_birth lead_status
I am only interested in importing a few of these and have the initialization as follows:
These are the classes describing the fields/columns I want, and the Mapper I thought would make it ignore the rest.
public class CSVFileVars
{
public string? full_name { get; set; }
public string? email { get; set; }
public string? phone_number { get; set; }
public DateTime date_of_birth { get; set; }
}
public sealed class CSVFileVarsMap : ClassMap<CSVFileVars>
{
public CSVFileVarsMap()
{
AutoMap(CultureInfo.InvariantCulture);
}
}
This is my code:
...
StreamReader reader;
List<CSVFileVars> records = new List<CSVFileVars>();
reader = new StreamReader(memStrm);
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
// NewLine = Environment.NewLine,
Delimiter = ",",
HeaderValidated = null,
MissingFieldFound = null,
IgnoreBlankLines = false,
HasHeaderRecord = true
};
using (var csv = new CsvReader(reader,config ))
{
csv.Context.RegisterClassMap<CSVFileVarsMap>();
records = csv.GetRecords<CSVFileVars>().ToList();
If I feed all the columns I am getting this exception:
Which seems odd that as looking at the exception it is referencing an Organisation object that is no where present in this controller or any of the data objects. Could the code be casting around to try to find things to map the unneeded columns to?
Also, it works fine if I strip out the columns that are not being used, which is a temporary work around but obviously a bit sub optimal.
I can’t help thinking I am doing something maybe obviously stupid, but this does seem odd.
So how do I get it to just ignore everything but the columns I want?