开发者

Linq to CSV select by column

If I开发者_运维技巧 have the following (sample) text file;

year,2008,2009,2010
income,1000,1500,2000
dividends,100,200,300
net profit,1100,1700,2300
expenses,500,600,500
profit,600,1100,1800

Is there a way in Linq that I can select the expenses for 2010 only?

So far I have the following which gets me all the data;

        var data = File.ReadAllLines(fileName)
            .Select( 
                l => {
                    var split = l.CsvSplit();
                    return split;
                }
            );

        foreach (var item in data)
            Console.WriteLine("{0}: ${1}", item[0], item[1]);


If you know it's always the 3rd value column, then

// the expenses row
var query = data.Single(d => d[0] == "expenses");

// the third column
return query[3];

and if you don't, then

var columnNumber = Array.IndexOf(data.First(), "2010");
return query[columnNumber];


See LINQtoCSV, its a library that does all this for you. I've used it, and it works like a charm.

http://www.codeproject.com/KB/linq/LINQtoCSV.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜