Reputation: 3
foreach (var file in d.GetFiles("*.csv"))
{
using (var reader = new StreamReader(file.FullName))
{
List<string> columns = new List<string>();
string colLine = File.ReadLines(file.FullName).First(); // gets the first line from file.
columns = colLine.Split(',').ToList();
reader.ReadLine(); // skip first line
dynamic x = new ExpandoObject();
var eoa = (IDictionary<string, object>)x;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
for (int idx = 0; idx < values.Length; idx++)
{
if (values[idx] == "\"\"")
{
values[idx] = "[BLANK]";
}
}
int i = 0;
foreach (var value in values)
{
List<string> val = new List<string>();
val.Add(value);
if (!eoa.ContainsKey(columns.ElementAt(i)))
{
eoa.Add(columns.ElementAt(i), val);
}
else
{
List<string> overwrite = new List<string>();
var curr = eoa[columns.ElementAt(i)];
foreach (var v in (dynamic)curr)
{
overwrite.Add(v);
}
overwrite.Add(value);
eoa[columns.ElementAt(i)] = overwrite;
}
i++;
}
}
using (StreamWriter outFile = new StreamWriter(path + @"\output.txt"))
{
foreach (var entry in eoa)
{
var test = entry.Value as List;
outFile.Write("{0}:{1} ", entry.Key, entry.Value);
}
}
}
}
I'm currently unable to get the value of an index belonging to the test variable. I try doing test[0] but get the error "Cannot apply indexing with [] to an expression of type 'object'. I am curious what other ways I can attempt so that these list elements can be accessed which belong to the Value of my dictionary.
Upvotes: 0
Views: 44
Reputation: 162
Your value seems to be of type object
, and as the error suggests you can't directly apply indexing to it. You have to cast it to a List<string>
to use test[0].
Upvotes: 1