質問

First, thank you for your time. I'm trying to use the CsvHelper for the first time along with a custom class and custom map. I'm getting "No cercion operator is defined between types 'System.Int32' and 'System.String'"

Here are my classes:

class Vendor
{
    public string VENDORID { get; set; }
    public string VENDORNAME { get; set; }
    public string VENDORSHORTNAME { get; set; }
    public string VENDORCHECKNAME { get; set; }
    public string HOLD { get; set; }
    public string VENDORSTATUS { get; set; }
    public string VENDORCLASSID { get; set; }
    public string PRIMARYVENDORADDRESSID { get; set; }
    public string VENDORCONTACT { get; set; }
    public string ADDRESS1 { get; set; }
    public string ADDRESS2 { get; set; }
    public string ADDRESS3 { get; set; }
    public string CITY { get; set; }
    public string STATE { get; set; }
    public string ZIPCODE { get; set; }
    public string COUNTRYCODE { get; set; }
    public string COUNTRY { get; set; }
    public string PHONE1 { get; set; }
    public string PHONE2 { get; set; }
    public string PHONE3 { get; set; }
    public string FAXNUMBER { get; set; }
    public string VENDORACCOUNTNUMBER { get; set; }
    public string PAYMENTTERMSID { get; set; }
    public string TAXIDNUMBER { get; set; }
    public string TAXREGISTRATIONNUMBER { get; set; }
    public string USER_DEFINED1 { get; set; }
    public string USER_DEFINED2 { get; set; }
    public string TAX1099TYPE { get; set; }
    public string TAX1099BOXNUMBER { get; set; }
    public string PURCHASESACCOUNT { get; set; }
    public string TRANSITROUTINGNUMBER { get; set; }
    public string EFTBANKACCOUNTNUMBER { get; set; }
}

class VendorMap : CsvClassMap<Vendor>
{
    public override void CreateMap()
    {
        Map(m => m.VENDORID).Name("Vendor ID");
        Map(m => m.VENDORNAME).Name("Vendor name");
        Map(m => m.VENDORSHORTNAME).Name("Vendor short name");
        Map(m => m.VENDORCHECKNAME).Name("Vendor check name");
        Map(m => m.HOLD).Name("Hold").Default(1);
        Map(m => m.VENDORSTATUS).Name("Vendor status").Default("Active");
        Map(m => m.VENDORCLASSID).Name("Vendor class ID");
        Map(m => m.PRIMARYVENDORADDRESSID).Name("Primary vendor address ID");
        Map(m => m.VENDORCONTACT).Name("Vendor contact");
        Map(m => m.ADDRESS1).Name("Address 1");
        Map(m => m.ADDRESS2).Name("Address 2");
        Map(m => m.ADDRESS3).Name("Address 3");
        Map(m => m.CITY).Name("City");
        Map(m => m.STATE).Name("State");
        Map(m => m.ZIPCODE).Name("Zip Code");
        Map(m => m.COUNTRYCODE).Name("Country code");
        Map(m => m.COUNTRY).Name("Country");
        Map(m => m.PHONE1).Name("Phone 1");
        Map(m => m.PHONE2).Name("Phone 2");
        Map(m => m.PHONE3).Name("Phone 3");
        Map(m => m.FAXNUMBER).Name("Fax number");
        Map(m => m.VENDORACCOUNTNUMBER).Name("Vendor account number");
        Map(m => m.PAYMENTTERMSID).Name("Payment terms ID");
        Map(m => m.TAXIDNUMBER).Name("Tax ID number");
        Map(m => m.TAXREGISTRATIONNUMBER).Name("Tax registration number");
        Map(m => m.USER_DEFINED1).Name("User-defined 1");
        Map(m => m.USER_DEFINED2).Name("User-defined 2");
        Map(m => m.TAX1099TYPE).Name("Tax 1099 type:");
        Map(m => m.TAX1099BOXNUMBER).Name("Tax 1099 box number");
        Map(m => m.PURCHASESACCOUNT).Name("Purchases account");
        Map(m => m.TRANSITROUTINGNUMBER).Name("Transit Routing Number");
        Map(m => m.EFTBANKACCOUNTNUMBER).Name("EFT Bank Account Number");
    }
}

Here is where I am reading a file:

CsvReader csv = new CsvReader(reader);

csv.Configuration.HasHeaderRecord = true;
csv.Configuration.RegisterClassMap<VendorMap>();

List<Vendor> vendors = null;

try
{
    vendors = csv.GetRecords<Vendor>().ToList();

Ideally, I would like the HOLD, TAX99TYPE, and TAX99BOXNUMBER properties types to be "short". However, with that I received the error "The conversion cannot be performed".

Here is part of my test CSV:

"Vendor ID","Vendor name","Vendor short name","Vendor check name","Hold","Vendor status","Vendor clas
"AA0011","UAT Demo","","","","Active","","Residence","Demo UAT","TESTING","TESTING","","TESTING","KS"
"AA0011","UAT Demo","","","","Active","","Supply","Demo UAT","NEW ADDRESS","NEW ADDRESS","","NEW ADDR
役に立ちましたか?

解決

The issue is essentially caused by mismatch of the types between column definition (string):

public string HOLD { get; set; }

and it's default value (Int32),

Map(m => m.HOLD).Name("Hold").Default(1);

As far as I'm aware, with the current version of CsvHelper, the default value type should match the type definition of its corresponding column.

You have two options . Either define column HOLD as short, or change the default value to be a string (ie "1").

Assuming you've already tried the former option, the error "The conversion cannot be performed" probably means that one or more column values in your CSV file contains a value which cannot be converted to a short (ie an alphanumeric value).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top