I have a csv file that I need to read and wanted to parse the result using linq.
At first I was using the following:
public dynamic ReadCSVData(string path, bool firstRowFields = true) { var query = from l in File.ReadAllLines(path) let f = l.Split(",".ToCharArray()) select new { Zero = f[0], One = f[1], Two = f[2], Three = f[3], Four = f[4], Five = f[5], }; return query.Skip(firstRowFields ? 1 : 0).ToList(); } |
This reads data that only has the fields separated with just a comma.
The file I am reading also has fields that are enclosed with quotes.
After doing some research I came across the TextFieldParser in the Microsoft.VisualBasic.dll.
After adding the dll to the project I was able to adjust the code and now it looks as follows:
public dynamic ReadCSVData(string path, bool firstRowFields = true) { dynamic data = null; // Have to add Microsoft.VisualBasic.dll to project using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(path)) { parser.HasFieldsEnclosedInQuotes = true; parser.Delimiters = new[] { "," }; var list = new List<string[]>(); while (parser.PeekChars(1) != null) { string[] fields = parser.ReadFields(); list.Add(fields); } var query = from f in list select new { Zero = f[0], One = f[1], Two = f[2], Three = f[3], Four = f[4], Five = f[5], }; data = query.Skip(firstRowFields ? 1 : 0).ToList(); } return data; } |
The second method may not be usable if the file is large or fast parsing is needed.
For my project it is a small file and speed is not an issue.
This parses the file correctly as well as being part the .Net framework.