I have a csv file with repetitive keys and multiple values from a csv. I have successfully parsed every row of csv in C#
My csv file looks like this
eid,hr
ABC,25
ABC,35
FDG,50
LMN,61
Task1 I would want to construct a dictionary
like Dictionary<string,int[]> or Dictonary<string,List>
key1: ABC value1: 25
key2: ABC value2: 35
key3: FDG value3: 50
key4: LMN value4: 61
Task 2
Iterate through keys and make sure the total of the values for the same key is more than 50. value1 and value2 will be proportioned to make the total as 50.
Here is the solution:
namespace CSVRead {class Program
{
static void Main(string[] args)
{
string[] csvLines = System.IO.File.ReadAllLines("//TheCSVfilepath");
var eID = new List<string>();
var hr = new List<double>();
for ( int i = 1; i < csvLines.Length; i++)
{
string[] rowData = csvLines[i].Split(',');
eID.Add(rowData[0]);
double hre = Convert.ToDouble(rowData[7]);
hr.Add(hre);
}
Console.WriteLine("eidDict: ");
for ( int m =0 ; m < eID.Count ; m++)
{
List<(string Key, double Value)> list = new List<(string Key, double Value)>; //CS1526Error
var eIDdictlist = list;
for (int n =0; n < hr.Count; n++)
{
employedictlist.Add(new KeyValuePair<string, double>(eID[m], hr[n])); //CS1503 Error
Console.WriteLine(eIDdictlist);
}
}
Console.ReadKey();
}
}