34

I want to get sum of the values from list.

For example: I have 4 values in list 1 2 3 4 I want to sum these values and display it in Label

Code:

protected void btnCalculate_Click(object sender, EventArgs e)
{
    string monday;
    TextBox txtMonTot;
    List<string> monTotal = new List<string>();

    if (Application["mondayValues"] != null)
    {
        List<string> monValues = Application["mondayValues"] as List<string>;
        for (int i = 0; i <= gridActivity.Rows.Count - 1; i++)
        {
            GridViewRow row = gridActivity.Rows[i];
            txtMonTot = (TextBox)row.FindControl("txtMon");
            monday = monValues[i];
            monTotal.Add(monday);
        }
    }
}

Any ideas? Thanks in advance

Marc
  • 3,819
  • 4
  • 22
  • 34
user2500094
  • 973
  • 6
  • 22
  • 42
  • 9
    **list.Sum()** . – I4V Sep 16 '13 at 09:37
  • 4
    Since this comment is being upvoted, it should be mentioned that as others have commented, you need a reference to System.Linq to use list.Sum(). – OldDog Dec 12 '16 at 05:44
  • Possible duplicate of [C# List of objects, how do I get the sum of a property](https://stackoverflow.com/questions/4351876/c-sharp-list-of-objects-how-do-i-get-the-sum-of-a-property) – Brian Hooper Aug 30 '17 at 09:21

4 Answers4

86

You can use the Sum function, but you'll have to convert the strings to integers, like so:

int total = monValues.Sum(x => Convert.ToInt32(x));
Sriram Sakthivel
  • 69,953
  • 7
  • 104
  • 182
Roy Dictus
  • 31,619
  • 8
  • 58
  • 72
  • 2
    If they have to be summed, why are the lists of string in the first place? – Abhitalks Sep 16 '13 at 09:40
  • 3
    You probably don't need the lambda `Sum(Convert.ToInt32)` – Sayse Sep 16 '13 at 09:42
  • 6
    To clarify, @Sayse is not wrong, and the syntax is valid. The problem is Sum has two overloads, one which accepts a func that returns a decimal and one that accepts a func that returns a Nullable. With the method group syntax, it can't tell which overload Convert.ToDecimal matches. The lambda syntax works around the problem wrapping Convert.ToDecimal in another method which always returns a decimal. – Tim Copenhaver Sep 17 '13 at 13:41
  • 1
    Make sure to add `using System.Linq;`. – Justin Helps Oct 09 '20 at 16:03
17

Use Sum()

 List<string> foo = new List<string>();
 foo.Add("1");
 foo.Add("2");
 foo.Add("3");
 foo.Add("4");

 Console.Write(foo.Sum(x => Convert.ToInt32(x)));

Prints:

10

DGibbs
  • 13,782
  • 7
  • 43
  • 81
  • I was just about to ask the same – NDJ Sep 16 '13 at 09:44
  • 1
    I really don´t see why this answer should get downvoted! Maybe he read this Question http://meta.stackexchange.com/questions/17204/six-simple-tips-to-get-stack-overflow-reputation-fast and applied rule no. 2 – makim Sep 16 '13 at 09:45
8

You can use LINQ for this

var list = new List<int>();
var sum = list.Sum();

and for a List of strings like Roy Dictus said you have to convert

list.Sum(str => Convert.ToInt32(str));
makim
  • 3,014
  • 3
  • 29
  • 47
2

How about this?

List<string> monValues = Application["mondayValues"] as List<string>;
int sum = monValues.ConvertAll(Convert.ToInt32).Sum();
Prasad Kanaparthi
  • 6,154
  • 3
  • 32
  • 60