Question

I'm writing a small application that will download a .csv file via FTP and read that into Excel in a particular format.

I found the LINQ code snippet below on this site that will read the .csv file into the var 'csv'. The problem is that I can't seem to figure out how to enumerate the var 'csv' into a string array (which I'll then use to populate the relevant Excel cells).

Can anyone help? Thanks, Gavin

var lines = File.ReadAllLines(lblShowFileName.Text).Select(a => a.Split(','));
var csv = from line in lines select (from piece in line select piece);
Was it helpful?

Solution

Use ToArray() extension method to generate string[] instead of IEnumerable<string>

var csv = (from line in lines
           select (from piece in line
                   select piece).ToArray()).ToArray();

Because you're calling ToArray() twice - within inner and outer query, your csv variable will be jagged array of strings: string[][]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top