Linq to CSV按列值选择

时间:2011-01-06 02:00:10

标签: linq

我知道我今天早些时候以不同的方式问过question,但我已经更好地完善了我的需求。

给出以下csv文件,其中第一列是标题,可以有任意数量的列;

year,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017
income,1000,1500,2000,2100,2100,2100,2100,2100,2100,2100
dividends,100,200,300,300,300,300,300,300,300,300
net profit,1100,1700,2300,2400,2400,2400,2400,2400,2400,2400
expenses,500,600,500,400,400,400,400,400,400,400
profit,600,1100,1800,2000,2000,2000,2000,2000,2000,2000

如何选择给定年份的利润值?因此,我可以提供一年的2011年,并期望获得2000年的利润值。

目前我有这个显示每年的利润值,但理想情况下我想指定年份并获得利润值;

        var data = File.ReadAllLines(fileName)
            .Select( 
                l => {
                    var split = l.Split(",".ToCharArray());
                    return split;
                }
            );

        var profit = (from p in data where p[0] == profitFieldName select p).SingleOrDefault();
        var years = (from p in data where p[0] == yearFieldName select p).FirstOrDefault();
        int columnCount = years.Count() ;

        for (int t = 1; t < columnCount; t++)
            Console.WriteLine("{0} : ${1}", years[t], profit[t]);

2 个答案:

答案 0 :(得分:1)

你有一个不幸的数据格式,但我认为最好的办法是定义一个类,创建一个列表,然后使用你的输入创建要添加到列表中的对象。然后你可以做任何你需要的查询来获得你想要的结果。

class MyData
{
    public string Year { get; set; }
    public decimal Income { get; set; }
    public decimal Dividends { get; set; }
    public decimal NetProfit { get; set; }
    public decimal Expenses { get; set; }
    public decimal Profit { get; set; }
}

// ...

string dataFile = @"C:\Temp\data.txt";
List<MyData> list = new List<MyData>();

using (StreamReader reader = new StreamReader(dataFile))
{
    string[] years = reader.ReadLine().Split(',');
    string[] incomes = reader.ReadLine().Split(',');
    string[] dividends = reader.ReadLine().Split(',');
    string[] netProfits = reader.ReadLine().Split(',');
    string[] expenses = reader.ReadLine().Split(',');
    string[] profits = reader.ReadLine().Split(',');

    for (int i = 1; i < years.Length; i++) // index 0 is a title
    {
        MyData myData = new MyData();
        myData.Year = years[i];
        myData.Income = decimal.Parse(incomes[i]);
        myData.Dividends = decimal.Parse(dividends[i]);
        myData.NetProfit = decimal.Parse(netProfits[i]);
        myData.Expenses = decimal.Parse(expenses[i]);
        myData.Profit = decimal.Parse(profits[i]);

        list.Add(myData);
    }
}

// query for whatever data you need
decimal maxProfit = list.Max(data => data.Profit);

答案 1 :(得分:1)

我今天已经回答了这个问题,但这个答案有点充实,希望更清楚。

string rowName = "profit";
string year = "2011";

var yearRow = data.First();
var yearIndex = Array.IndexOf(yearRow, year);

// get your 'profits' row, or whatever row you want
var row = data.Single(d => d[0] == rowName);

// return the appropriate index for that row.
return row[yearIndex];

这适合我。