Question

I'm trying to learn how to use LINQTOExcel to query a CSV file. Following the tutorial on the site I adapted their example to work with my data (filename is passed to it via an OpenDialog component):

var csv  = new ExcelQueryFactory(filename);
var test = from c in csv.Worksheet<TestData>()
           select c;

foreach(var t in test)
{
    Console.WriteLine(t.Contract_Id);
}

I've got a separate TestData class/model which looks like this:

class TestData
{
    public string Transaction_Id { get; set; }

    public string Value_Date { get; set; }

    public string Transmit_Date { get; set; }

    public string Transmit_Time { get; set; }

    public string Contract_Id { get; set; }

    public string Contract_Amount { get; set; }

    public string Contract_Rage { get; set; }

    public string TestAmount { get; set; }

    public string Employer_Code { get; set; }

    public string Test_Acceptor { get; set; }

    public string Institution_Id { get; set; }
}

But when I loop through, all of the values for each item are 'null'. Am I missing a step somewhere?

Example CSV Data:

transaction_id,value_date,transmit_date,transmit_time,contract_no,contract_amount,instalment,test_amount,employer_code,test_acceptor,institution_id
35454521,20111230,20120102,2:23:12,1442,1714.56,1,285.76,0,643650,a
Était-ce utile?

La solution

The CSV file needs a header row that matches the property names:

Transaction_Id,Value_Date,Transmit_Date,Transmit_Time,Contract_Id,Contract_Amount,Contract_RageTestAmount,Employer_Code,Test_Acceptor,Institution_Id
35454521,20111230,20120102,2:23:12,1442,1714.56,1,285.76,0,643650
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top