문제

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);
도움이 되었습니까?

해결책

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[][]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top