0

I am trying to get the information from this file (indexes.csv):

enter image description here

Into an instance of this class:

class Table
{
    Dictionary<String, double> _regionTimeValues;
    String _region;

    public Table(String region)
    {
        _regionTimeValues = new Dictionary<string, double>();
        _region = region;
        suckInValues();
    }

    private void suckInValues()
    {
        //Go find File, get the appropriate Values for _region
        //add each value found in the csv file that applies, indexed by yearQuarter
        //Example: _regionTimeValues.Add("2013Q1", 1.1 );
    }

    internal double locateRelevantValue(string yearQuarter)
    {
        double locatedValue = 0.0;
        _regionTimeValues.TryGetValue(yearQuarter,out locatedValue);
        return locatedValue;
    }

I want to only fill the dictionary with the specific region's data.

How can I do this from a csv file?

EDIT

An example of region would be a string value like "Dalton"

jth41
  • 3,808
  • 9
  • 59
  • 109

2 Answers2

2

I think you want something like this...

public class Table
{
    private Dictionary<string, double> _regionTimeValues = new Dictionary<string, double>();
    private String _region;

    public Table(String region)
    {
        _region = region;
    }

    public void AddValue(string key, double value)
    {
        _regionTimeValues.Add(key, value);
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, Table> tables = new Dictionary<string, Table>();

        using (var reader = new StreamReader("Data.csv"))
        {
            // First line contains column names.
            var columnNames = reader.ReadLine().Split(',');
            for(int i = 1; i < columnNames.Length; ++i)
            {
                var columnName = columnNames[i];
                tables.Add(columnName, new Table(columnName));
            }

            var line = reader.ReadLine();
            while (line != null)
            {
                var columns = line.Split(',');

                for (int i = 1; i < columns.Length; ++i)
                {
                    var table = tables[columnNames[i]];
                    table.AddValue(columns[0], double.Parse(columns[i]));
                }

                line = reader.ReadLine();
            }
        }
    }
}
MrMoDoJoJr
  • 390
  • 3
  • 9
0

First, you would read the headers and get the index of the columns that contain your region. Then you will have to read each line and split the line by ',' and then read the index to get your value. You can also apply yr\qrt to it by searching the first index of the split record.

Justin
  • 3,337
  • 3
  • 16
  • 27