Assume I have a large csv file that I'd like to read, modify and write back. Perhaps I want to change a field separator from comma to tab. Perhaps I want to change the quotation character from ' to ". Or may be I want to add a plus sign to each value in the first column. Since the file is large, I do not want to load it in memory all at once, I'd like to read it record by record.
So I write a code like this:
var inPath = @"infile.txt";
var outPath = @"outfile.txt";
CsvConfiguration readCf = GetReadConfiguration();
CsvConfiguration writeCf = GetWriteConfiguration();
using (var streamin = new FileStream(inPath, FileMode.Open))
using (var streamReader = new StreamReader(streamin))
{
using (var csvReader = new CsvReader(streamReader, readCf))
using (var csvWriter = new CsvWriter(new StreamWriter(outPath), writeCf))
{
while (csvReader.Read())
{
var currentRecord = csvReader.GetRecord<dynamic>();
UpdateRecord(currentRecord);
csvWriter.WriteRecord(currentRecord);
}
}
}
This fails at run time with the following error:
Types that inherit IEnumerable cannot be auto mapped. Did you accidentally call GetRecord or WriteRecord which acts on a single record instead of calling GetRecords or WriteRecords which acts on a list of records?
Note, that nothing interesting happens in UpdateRecord
, in fact this line above can be completely commented out.
What happens is that GetRecord
returns an ExpandoObject
and then WriteRecord
chokes on this object.
What is the best way to make this work?
Update
Just so it's clear: I fully realize that I'm getting this error is because CSVHelper does not support ExpandoObject
s for WriteRecord
call. I'm looking for suggestions, for easiest way to make this work.