Suppose I have a large csv file that I would like to read, modify and write. Perhaps I want to change the separator of the semicolon fields to a tab. Perhaps I want to change the quote symbol from "to". Maybe I want to add a plus sign to each value in the first column. Since the file is large, I don’t want to load it into memory right away. I would like to read a record by record.
So, I am writing this code:
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 runtime with the following error:
Types that inherit IEnumerable cannot be automatically matched. Have you accidentally called GetRecord or WriteRecord, which acts on one instead of calling GetRecords or WriteRecords, which acts on the list of records?
, UpdateRecord , .
, , GetRecord ExpandoObject, WriteRecord .
?
: , , CSVHelper ExpandoObject WriteRecord . , .