Hi All,
I have a case were i have to remove the multiple headers present in the file at different levels of the whole file.
Eg
Customer,Address,PhoneNo
abc,ewq,1234
fdg,sdfsd,4234
Customer,Address,PhoneNo
fdsad,fsas,45345
fsdgf,fdhfdg,5656
So now i have to remove rest of the headers from the file present in the subsequent rows if any and keep only the inital first header which is at the start of the file.
we can readin all lines into an array and then filter out with LINQ all lower lines which are same as the first header line. Let us know if you need more help on this
arrLines = File.ReadAllLines(Path.Combine(Environment.CurrentDirectory,"__Tasks\CSV\RemoveRepeatingHeaderLines","Data.csv"))
arrFilteredLines = arrLines.Where(Function (x) Not x.Equals(arrLines(0))).Prepend(arrLines(0)).toArray
OR
arrFilteredLines = arrLines.Except({arrLines(0)}).Prepend(arrLines(0)).toArray
The result we can pass to generate Datatable with a String.Join(Environment.NewLine, arrFilteredLines) or can write out to a text file for later readin with read csv file.
Tried using that but it is string array and write text except a string … looping through the whole file will consume a lot of time if it has more lines
just make a joined string with the String.Join method
Also have a look on the File.WriteAllLine method like: File.WriteAllLines(Path,arrLinesFiltered);