How to calculate sum of items in a List<List<string>> using C# -


i have list<list<string>> .each item in these list, used display columns of report. need calculate sum (after converting string decimal),of each each column in report , show footer. not sure, number of items in each list match other lists.

that means, each item of lists should converted , sum , add in list, used display footer row.

please suggest me, method better use achieve in performance wise.

 list<list<string>> test = new list<list<string>>();  list<string> columntotal = new list<string>();  decimal total = 0;  foreach (var item in test)  {  //get first element  // decimal dec = convert.todecimal(first element);  // total =total +dec ; ....  //once first element in list has been added,  //columntotal .add(convert.tostring(total));  //now second element ...so on  } 

this actual requirement.

give try. assume you're using 2d table , not jagged table.

// assuming 2d table , not jagged list<list<string>> table = new list<list<string>> {     new list<string> { "1", "2", "3" },                   new list<string> { "1", "2", "3" },     new list<string> { "1", "2", "3" },     new list<string> { "2", "3", "4" } };  list<decimal> footertotals = new list<decimal>();  (int = 0; < table[0].count; i++) {     // sum columns     footertotals.add(table.sum(t => decimal.parse(t[i]))); }  table.foreach(row => console.writeline(string.join("\t", row))); console.writeline(string.join("\t", footertotals)); 

results:

1       2       3 1       2       3 1       2       3 2       3       4 5       9       13 

Comments