字符串类型的DataTable列的总和 [英] Sum DataTable columns of type string

查看:64
本文介绍了字符串类型的DataTable列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算 DataTable 中字符串类型为两行的总和?

我不能更改列类型。

我尝试了这些:

How do I calculate Sum of two rows in DataTable which is of type string ?
in my case I cannot change column type.
I tried these :

1)

object objSum;    
objSum = dt.Compute("Sum(WeightRate)", "");     

2)

decimal total = (decimal)dt.Compute("SUM( [WeightRate] )", "");`   

但是没有运气...

我的桌子:


车辆重量编号

350.50 50

205.00 40

Weightrate No.Ofvehicles
350.50 50
205.00 40

I需求结果为


体重编号的车辆

555.50 90

Weightrate No.Ofvehicle
555.50 90


推荐答案

Compute方法允许进行一些基本的类型转换:

The Compute method allows for some rudimentary type conversion:

var sum = dt.Compute("Sum(Convert(WeightRate, 'System.Int32'))");

请参见数据列表达式,详细了解如何使用Compute方法调用。

See the MSDN help on DataColumn Expressions for more information about what you can do with a Compute method call.

编辑重新阅读您的问题后,您可能希望转换为System.Decimal而不是System.Int32,但希望您能成功。 ;-)

Edit Having re-read your question, you'll probably want to convert to System.Decimal rather than System.Int32, but hopefully you got that. ;-)

这篇关于字符串类型的DataTable列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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