Hello, I am trying to split a CSV by a source in the data but keep the headers. The orginal code I had was -
var splitQuery = from line in File.ReadLines(@"C:\test\test1.csv")
let source = line.Split(',').Last()
group line by source into outputs
select outputs;
foreach (var output in splitQuery)
{
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", output);
}
This worked however it didnt include the headers in the split files, so I have tried this -
var headerLine = File.ReadLines(@"C:\test\test1.csv");
var nHeaderLine = headerLine.Take(1);
string FHeader = nHeaderLine.ToString();
var splitQuery = from line in headerLine
let source = line.Split(',').Last()
group line by source into outputs
select outputs;
foreach (var output in splitQuery)
{
var Foutput = FHeader + "\r\n" + output;
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);
}
Im probably doing this completely wrong but im getting two errors (so far) and these are
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);
Error 1 The best overloaded method match for 'System.IO.File.WriteAllLines(string, System.Collections.Generic.IEnumerable<string>)' has some invalid arguments C:\Users\nicoles\Documents\Visual Studio 2013\Projects\splitTest\splitTest\Program.cs 32 17 splitTest
Error 2 Argument 2: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>' C:\Users\nicoles\Documents\Visual Studio 2013\Projects\splitTest\splitTest\Program.cs 32 71 splitTest
Could someone help me out please
What I have tried:
var headerLine = File.ReadLines(@"C:\test\test1.csv");
var nHeaderLine = headerLine.Take(1);
string FHeader = nHeaderLine.ToString();
var splitQuery = from line in headerLine
let source = line.Split(',').Last()
group line by source into outputs
select outputs;
foreach (var output in splitQuery)
{
var Foutput = FHeader + "\r\n" + output;
File.WriteAllLines(@"C:\test\" + output.Key + ".csv", Foutput);
}