Given the following code snip:
using (var reader = cmd.ExecuteReader())
{
while(reader.Read())
{
var count = reader.FieldCount; // the first column must be name all subsequent columns are treated as data
var data = new List<double>();
for (var i = 1; i < count; i++)
{
data.Add(Convert.ToDouble(reader[i].ToString()));
}
barChartSeries.Add(new ColumnBarChart.ColumnChartSeries((string)reader[0],
data));
columnChart.xAxis.categories.Add((string)reader[0]);
}
}
Is there an easy way to eliminate the for loop? Perhaps using linq?
reader[0] will always be a string reader[0+?] will be a double
I want to pull all the doubles into a list if possible.
GetDoubleinstead), but the rest seems okay to me...forwithEnumerable.Rangeand do it withLinq, but basically you are just swapping aforwith aforeachin that case, your code seems fine to me, I can see no benefit of refactoring out theforloopGetDoublethere's only one thing to do: init your list withcount - 1as capacity to reduce reallocations, or even use fixed-size array instead. Also, useGetStringfor column 0.