0

I have a Business Object class containing private fields which are populated in the constructor

private int mP_Id;
private int mP_MerchantId;
private int mP_ProductId;
private decimal mP_Price;
private int mP_StockCount;
private string mP_Condition;
private bool mP_Published;
private int? mP_ConditionId;
private string mP_URL;
private string mP_CurrencyCode;
private DateTime? mP_DateUpdated;
private bool mP_Active;
private string mP_MerchantProductIdentifier;
private int? mP_ApiProductFoundId;

I have a CSV file that contains these columns but also has additional columns that will be used in the business logic to retrieve the correct mP_ProductId from another database table.

If I use CsvContext.Read<T>() it throws an exception telling me that the extra columns are not found in my Business Object.

                var cc = new CsvContext();
                var relationships = cc.Read<MerchantProductRelBO>(file.FilePath, inputFileDescription);

I tried to place these extra fields and overload the constructor in the MerchantProductRelBO.custom.cs but it still didn't use the overloaded constructor and threw the same exception. I can't do this in the MerchantProductRelBO.cs because the file gets overwritten anytime the database structure changes.

Do I just need to abandon the LinqToCsv library and write a manual parser to read from the CSV or is there a way to get it to parse the extra-BO columns without throwing an exception?

objecttothis
  • 45
  • 1
  • 7
  • Extend the class and add your own fields, then use that. Assuming the BO class isn't marked final/sealed...if it is, use composition instead. – Nikki9696 Apr 16 '21 at 14:13
  • It really helps if you say where `CsvContext.Read()` comes from and / or what LinqToCsv library you are using exactly. – NetMage Apr 16 '21 at 21:12
  • Assuming you are using [LinqToCsv](https://www.nuget.org/packages/LinqToCsv/) (which is quite old), the documentation says you can create a `CsvFileDescription` with the property `IgnoreUnknownColumns` set to `true` and pass it as the second parameter. – NetMage Apr 16 '21 at 21:16
  • [CsvHelper](https://github.com/JoshClose/CsvHelper) seems like a nice new library. – NetMage Apr 16 '21 at 21:23

0 Answers0