聚合函数 Sum() 和类型的无效使用:字符串 [英] Invalid usage of aggregate function Sum() and Type: String
本文介绍了聚合函数 Sum() 和类型的无效使用:字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据表,一列包含 int 值.我没有指定任何数据类型的列.当我执行以下操作时.
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.Compute("sum(Value)", "");
我收到以下错误.
聚合函数 Sum() 和类型:字符串的使用无效.
我尝试使用
object sum = dttest.Compute("sum(Convert(Value, 'System.Int32'))","");
我又犯了一个错误
聚合参数中的语法错误:需要单列参数可能带有Child"限定符.
当我为列指定数据类型时,第一个代码将返回正确的值.但就我而言,我无法指定列数据类型.有人有解决方案吗?
When I specify the datatype for the column, first code will return correct value. But in my case I can't specify column datatype. Any one having solution ?
推荐答案
您可以使用 LINQ to Datatable:
You can use LINQ to Datatable:
var result = dttest.AsEnumerable()
.Sum(x => Convert.ToInt32(x["Value"]));
示例如何对特定名称进行Sum
:
Example how to Sum
for specific name:
var result = dttest.AsEnumerable()
.Where(r => r.Field<string>("Name") == "FilterName")
.Sum(x => Convert.ToInt32(x["Value"]));
这篇关于聚合函数 Sum() 和类型的无效使用:字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文