I am trying to figure out how to split a file by the number of lines in each file. THe files are csv and I can\'t do it by bytes. I need to do it by lines. 20k seems to be a goo
I'd do it like this:
// helper method to break up into blocks lazily
public static IEnumerable> SplitEnumerable
(IEnumerable Sequence, int NbrPerBlock)
{
List Group = new List(NbrPerBlock);
foreach (T value in Sequence)
{
Group.Add(value);
if (Group.Count == NbrPerBlock)
{
yield return Group;
Group = new List(NbrPerBlock);
}
}
if (Group.Any()) yield return Group; // flush out any remaining
}
// now it's trivial; if you want to make smaller files, just foreach
// over this and write out the lines in each block to a new file
public static IEnumerable> SplitFile(string filePath)
{
return File.ReadLines(filePath).SplitEnumerable(20000);
}
Is that not sufficient for you? You mention moving from position to position,but I don't see why that's necessary.