I have the following scenario: I have five fields in my database (name, email, address etc) and then another n fields which are user defined, for instance I can add custom fields such as ID Number, Blood type or anything for that matter and as many of them as I like. I now need to put this into a CSV file. However the problem is that now all of the fields are required therefore I cannot just add the headers to the CSV file and then sequentially add the data as the data might not be in the correct column. What I would need to do is something along the lines of this:
-
Add the predefined headers
-
Add the rest of the custom headers
-
Add the predefined data
-
Add the custom data
My problem is step 4. What I would need is to be able to add the data to a specified column, and I cannot seem to find out how to do this in KBCSV.
What I am currently doing is:
List<string> data = new List<string>();
//Add all predefined data to the data object
foreach (CustomDataItem cdi in CustomData)
{
data.Add(cdi.Value);
}
writer.WriteDataRecord(data);
//writer is a Kent.Boogaart.KBCsv.CsvWriter
The problem with this approach is that the CustomData length and the csv header length could not match up, meaning I will have values in the wrong columns. Is there any way to add a data value to a specified column?
why not use a dictionary here, and then when writing in csv file check if key is present for the coreect sequence column in dictionary