开发者

Linq to CSV select by column value

I know I have asked this question in a different manner earlier today but I have refined my needs a little better.

Given the following csv file where the first column is the title and there could be any number of columns;

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

How do I select the profit value for a given year? So I may provide a year of say 2011 and expect to get the profit value of 2000 back.

At the moment I have this which shows the profit value for each year but ideally I'd like to specify the year and get the profit value;

        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]);


I've already answered this once today, but this answer is a little more fleshed out and hopefully clearer.

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

This works for me.


You have an unfortunate data format, but I think the best thing to do is just to define a class, create a list, and then use your inputs to create objects to add to the list. Then you can do whatever querying you need to get your desired results.

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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜