39

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);
        }
    }
}
starball
  • 20,030
  • 7
  • 43
  • 238
user2500094
  • 1,033
  • 6
  • 23
  • 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

98

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
  • 72,067
  • 7
  • 111
  • 189
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • 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
  • Just to clarify if you are going to use the `List().Sum()` method you have to add `using System.Linq;` to the top of your code – Gharbad The Weak Sep 02 '22 at 17:47
18

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
  • 14,316
  • 7
  • 44
  • 83
  • 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
10

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,154
  • 4
  • 30
  • 49
2

How about this?

List<string> monValues = Application["mondayValues"] as List<string>;
int sum = monValues.ConvertAll(Convert.ToInt32).Sum();
Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62