C#中 - 如何从列表中获取值的总和? [英] c# - How to get sum of the values from List?

查看:1117
本文介绍了C#中 - 如何从列表中获取值的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表中获取值的总和。

I want to get sum of the values from list.

例如:我在清单4的值
1 2 3 4我想总结这些值和标签显示它

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

推荐答案

您可以使用总和的功能,但你必须到字符串转换为整数,如所以:

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));

这篇关于C#中 - 如何从列表中获取值的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆