Datatable的计算方法不起作用 [英] Compute method of Datatable not working

查看:132
本文介绍了Datatable的计算方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想找到DataTable列的总和。通过使用计算方法。



我的表达式如下: -

  object  taxObject = tbl_Resords.Compute(  Sum(SERVICE TAX) ); 





但它会抛出异常。

 [System.Data.SyntaxErrorException] = {聚合参数中的语法错误:期望具有可能'Child'限定符的单个列参数。} 

解决方案

您好,



只是不要将您的INT列与空字符串进行比较!!



所以请使用以下内容:

 对象 taxObject = tbl_Resords.Compute(  Sum(SERVICE TAX)  [SERVICE TAX]> 0); 





  object  taxObject = tbl_Resords.Compute(  Sum(SERVICE TAX)  [SERVICE TAX] IS NOT NULL); 





同时检查DataTable中的列是否分配了正确的数据类型



希望这有帮助!! :)



问候,

Praneet


这里有两个主要参数需要传递给DataTable的'计算'方法是: -



1.第一个参数 - 参数为字符串的Agregate函数 - 这里函数是'Sum'但是,参数传递它应该是我们需要应用聚合的DataTable列名。



2.如果你不需要按照它来过滤它,那么第二个参数将保持空白任何表达。



所以,如果我们在这里看到你已经将价值'SERVICE TAX'传递给方法'Sum',主要是我猜的是这里的问题方法'Sum'不会接受包含其中任何空格的列名。



请传递正确的列名称以获得正确的结果。



这是一个适合我的例子: -



 DataTable dt =  new  DataTable(); 
受保护 void Page_Load( object sender,EventArgs e)
{

if (!Page.IsPostBack)
{
dt .Columns.Add( 名称 typeof String ));
dt.Columns.Add( 年龄 typeof int ));

DataRow dr1 = dt.NewRow();
dr1 [ 名称] = name 1;
dr1 [ 年龄] = 20 ;
dt.Rows.Add(dr1);

DataRow dr2 = dt.NewRow();
dr2 [ 名称] = name 2;
dr2 [ 年龄] = 30 ;
dt.Rows.Add(dr2);

object val = dt.Compute( Sum(Age) );
int agetotal = Convert.ToInt32(val);
}





希望这对您有帮助。


taxObject = tbl_Resords.Compute(Sum([SERVICE TAX]),);



这是我的解决方案。

谢谢

Hello I want to find sum of DataTable column. by using compute method.

My expression is as below :-

object taxObject = tbl_Resords.Compute("Sum(SERVICE TAX)", " ");



but it throws following exception.

[System.Data.SyntaxErrorException] = {"Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier."}

解决方案

Hi,

Just don't compare your INT column to an empty string!!

So use something like this:

object taxObject = tbl_Resords.Compute("Sum(SERVICE TAX)", "[SERVICE TAX] > 0");


Or

object taxObject = tbl_Resords.Compute("Sum(SERVICE TAX)", "[SERVICE TAX] IS NOT NULL");



Also check that the Column in your DataTable have proper DataTypes assigned

Hope this helps !! :)

Regards,
Praneet


Here two main arguments which needs to be passed to 'Compute' method of the DataTable are :-

1. First Argument - Agregate function with parameter as string - Here the function is 'Sum' but, the argument passed to it should be the DataTable column name on which we need to apply the aggregation.

2. Second argument will remain blank if you do not need to filter it as per any expression.

So if we will see here you have passed value 'SERVICE TAX' to method 'Sum' which mainly i guess is the problem here method 'Sum' will not accept it a column name which contains any white space within it.

Please pass the correct column name to get proper result.

Here is an example which worked for me :-

DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                dt.Columns.Add("Name", typeof(String));
                dt.Columns.Add("Age", typeof(int));

                DataRow dr1 = dt.NewRow();
                dr1["Name"] = "name 1";
                dr1["Age"] = 20;
                dt.Rows.Add(dr1);

                DataRow dr2 = dt.NewRow();
                dr2["Name"] = "name 2";
                dr2["Age"] = 30;
                dt.Rows.Add(dr2);

                object val = dt.Compute("Sum(Age)", "");
                int agetotal = Convert.ToInt32(val);
            }



Hope this will definitely of help to you.


taxObject = tbl_Resords.Compute("Sum([SERVICE TAX])", " ");

this is my solution.
Thank you


这篇关于Datatable的计算方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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