Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fields 'workOrder' do not exist in the CSV file

Tags:

c#

csv

csvhelper

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?

like image 460
haosmark Avatar asked Feb 10 '14 06:02

haosmark


1 Answers

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();
like image 85
Stadub Dima Avatar answered Oct 03 '22 01:10

Stadub Dima