istevens
istevens

Reputation: 21

Any way to read a CSV into an array or datatable using linq?

I have a long(ish) csv that I need to ingest into either a multi dimensional array or data table to be iterated over. The format is simple:

filepathX, sizeinbytes1 (i.e. C:\temp\yourfile.txt, 12345)

In straight .net I used the OleDb provider, however the Unity engine does not seem to play nice with this reference. I cannot use external libraries like FileHelpers.

Upvotes: 2

Views: 857

Answers (1)

dsz
dsz

Reputation: 5212

dasblinkenlight is right about CVS being (in general) non-trivial to parse, but you have described a VERY specific format.

Personlly, I'd just read the file line by line and use LastIndexOf(",") to split off the size - that at least will protect you from the possability that there's a comma in the file name.

Fianlly, do you really need it in an array or data table? You could wrap the file reading into your own iterator using "yield return" (return a custom struct or a Tuple if you're lazy!), and at least have "nice" code sitting on top doing the iteration.

Upvotes: 1

Related Questions