I have a CSV file with several rows and each row has a work order number under column titled "W.O.No."
Here's what I'm doing: I created a class called Job, with a single field.
class Job
{
public int workOrder { get; set; }
}
Then I created a JobMap class
class JobMap : CsvClassMap<Job>
{
public override void CreateMap()
{
Map(m => m.workOrder).Name("W.O.No.");
}
}
And finally, this is my main code
StreamReader file = new StreamReader(filePath);
var csv = new CsvReader(file);
var record = csv.GetRecords<Job>().ToList();
For whatever reason I'm getting an error saying
Fields 'workOrder' do not exist in the CSV file.
What am I doing wrong here?
Seems that in you code missing map initialization. Try to add csv.Configuration.RegisterClassMap<JobMap>();
This code should work:
StreamReader file = new StreamReader(filePath);
var csv = new CsvReader(file);
csv.Configuration.RegisterClassMap<JobMap>();
var record = csv.GetRecords<Job>().ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With