A slightly different way of doing what Karthik demonstrates here, showing the use of the 'yield iterator pattern:
Dictionary<int, List<double>> intdbl = new Dictionary<int, List<double>>
{
{1, new List<double>{1.0,2.0,3.0}},
{2, new List<double>{56.0,21.0,323.0}}
};
IEnumerable<double> listSums(List<List<double>> lstValues)
{
if (lstValues.Count == 0) yield break;
int repcount = lstValues[0].Count;
if (repcount == 0) yield break;
var okayvalues = lstValues.Where(lst => lst.Count == repcount);
if (okayvalues.Count() == 0)
{
yield break;
}
for (int i = 0; i < repcount; i++)
{
yield return
(okayvalues.Select(lst => lst[i]).Sum());
}
}
var test = listSums(intdbl.Values.ToList());</double>
The issue of what you can reliably assume is true about the state of the data you deal with here is ... critical. The above example would work with Dictionaries where the values (lists of double) are of any size, but, you can see that if you base the number of values to use on just the first list ... you have made an assumption that may come back to haunt you.
Assuming that you always want to have a number of "totals" equal to the size of the largest dictionary value (list of doubles), actually makes it simpler, imho, to program:
Dictionary<int, List<double>> intdbl = new Dictionary<int, List<double>>
{
{1, new List<double>{1.0,2.0,3.0}},
{2, new List<double>{56.0,21.0,323.0}},
{3, new List<double>{22.23, 12.435, 99.332, 120.453, 0.993}}
};
int maxlistsize = intdbl.Values.ToList()
.Select(itm => itm.Count).Max();
List<double> totals = new List<double>(maxlistsize);
totals.AddRange(Enumerable.Repeat(0d, maxlistsize));
foreach (List<double> dbllist in intdbl.Values)
{
for (int i = 0; i < dbllist.Count; i++)
{
totals[i] += dbllist[i];
}
}
> ? totals
Count = 5
[0]: 79.23
[1]: 35.435
[2]: 425.332
[3]: 120.453
[4]: 0.993</double></double></double>
The question remains however: what is your requirement here for structural consistency of the data ? When your code is used, what can you assume about the data, and what do those assumptions require to do in code to prevent errors and get "meaningful" results.