Invalid usage of aggregate function Sum() and Type: String

后端 未结 4 469
礼貌的吻别
礼貌的吻别 2020-12-21 15:25

I have a data table and a column contains int values. I have not specified the column with any datatype. When I perform the following.

object sum = dttest.Co         


        
4条回答
  •  春和景丽
    2020-12-21 15:57

    You can use LINQ to Datatable:

     var result = dttest.AsEnumerable()
                        .Sum(x => Convert.ToInt32(x["Value"]));
    

    Example how to Sum for specific name:

     var result = dttest.AsEnumerable()
                        .Where(r => r.Field("Name") == "FilterName")
                        .Sum(x => Convert.ToInt32(x["Value"]));
    

提交回复
热议问题