开发者

Sum Of a column of generic list

开发者 https://www.devze.com 2023-02-02 06:02 出处:网络
i have list of string 开发者_JAVA技巧like this : Dim totalPrice As New List(Of Integer) how can i get sum of column(0) (C# or VB) ?

i have list of string 开发者_JAVA技巧like this :

Dim totalPrice As New List(Of Integer)

how can i get sum of column(0) (C# or VB) ? thanks


In C#:

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


In C# do the following to conver the string list into a int list and the sum it.

List<string> list_str = new List<string>();
...
var list_int = list_str.Select( (x)=>int.Parse(x) );
int sum = list_int.Sum();


You wont be able to Sum a string but if it where a list of Integers

List<int> totalPrice = new List<int>();
var SumOftotalPrice = (from s in totalPrice
                       select s).Sum();


may be this should help

int sum = stringList.ConvertAll(Convert.ToInt32).Sum();

0

精彩评论

暂无评论...
验证码 换一张
取 消