I need to split large CSV files by the source field and name the export files the name as the source field.
My code works, but the only thing that's not working is I need the split files to have the header row from the original file.
Any help is appreciated. Thank you.
var splitQuery = from line in File.ReadLines(@"C:\test\test1.csv")
let source = line.Split(',').Last()
group line by source into outputs
foreach (var output in splitQuery)
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", output);
Simply read the header line first:
var fileLinesIterator = File.ReadLines(...); string headerLine = fileLinesIterator.Take(1);
Then prepend it to every output:
var splitQuery = from line in fileLinesIterator // ... File.WriteAllLines(@"C:\test\" + output.Key + ".csv", headerLine + "\r\n" + output);
But apart from that, you don't want to be handling CSV files as mere (lines of) strings. You're bound to running into running into trouble with quoted and multiline values.