如何使用Compute计算总数 [英] How to use the Compute to calculate the total

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

问题描述

我的桌子有:IDProduct,ProductName,Quantity,price,total

总计=价格*数量

我要总计列总和

Ex:

总计[0] =价格*数量(第一行)

总计[1] =价格*数量(第二行)

总计[2] =价格*数量

.....

我想得到:总计=总计[0] +总计[1] +总计[ 2] + .. +总计[n]

我不想用于/ foreach循环

我可以:

  float  TotalOrder =  float  .Parse(dt.Compute(  sum(total) string  .Empty).ToString ()); 



有人能帮助我吗?

解决方案

有两种选择。



选项1



表达式此处解释的 DataColumn 的属性 http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(V = VS .90).aspx [ ^ ]可用于为建立 Total = Quantity * Price 的关系总计列如下所示,在这种情况下总计字段将是只读字段。

 DataTable products =  new  DataTable( 产品); 
products.Columns.Add( IdProduct typeof int ), null );
products.Columns.Add( Quantity typeof double ), null );
products.Columns.Add( Price typeof double ), null );
products.Columns.Add( Total typeof double ), 价格*数量);

products.Rows.Add( 1 5 25 5 );
products.Rows.Add( 2 10 37 7 );


float TotalOrder = float .Parse(products.Compute ( sum(Total) string .Empty)的ToString());
Console.WriteLine(TotalOrder);
/ * 输出
产品内容表
Id数量价格总计
1 5 25.5 127.5
2 10 37.7 377
3 15 63.2 504.5

TotalOrder =
504.5
* /





选项2



LINQ 可按如下方式用于计算每个行的总计

 DataTable products =  new  DataTable( 产品); 
products.Columns.Add( IdProduct typeof int ), null );
products.Columns.Add( Quantity typeof double ), null );
products.Columns.Add( Price typeof double ), null );
products.Columns.Add( Total typeof double ), null );

products.Rows.Add( 1 5 25 5 );
products.Rows.Add( 2 10 37 7 );

// 计数扩展方法用于强制立即执行查询
// 因为LINQ查询具有延迟执行模型并被执行
< span class =code-comment> //
迭代时或调用像Count这样的标量函数。
products.AsEnumerable( )。选择(p = > {p.SetField< double>( 总计
p.Field< double>( 数量 )* p.Field< double>( Price));
return p;
})。Count();

float TotalOrder = float .Parse(products.Compute( sum(Total) string .Empty)的ToString());
Console.WriteLine(TotalOrder);
// 输出与上面显示的相同。


1。你为什么不只使用Linq?

  float  total =(浮动)dt.AsEnumerable()。总和(r => r.total); 



2.尝试使用大写 S 总之。

 dt.Compute( 总和(总计)字符串 .Empty)





3.我会把我的智能护目镜说成你应该重构你的代码...

(如果 Compute 返回null怎么办?为什么解析方法? ?)


试试这个



公共对象SumTotPremium;



  //  以下计算 
SumTotPremium = datatable.Compute ( Sum(Columname) );
// 显示到标签
lblshowTotPremium.Text =
< span class =code-sdkkeyword> String .Format( {0:INR#,## 0.00},Convert.ToDecimal(SumTotPremium));


My table has:IDProduct,ProductName,Quantity,price,total
total=price*Quantity
I want to sum the total column
Ex:
total[0]=price*Quantity (first row)
total[1]=price*Quantity (second row)
total[2]=price*Quantity
.....
I want to get:total=total[0]+total[1]+total[2]+..+total[n]
I don't want to use for/foreach loop
I can:

float TotalOrder = float.Parse(dt.Compute("sum(total)",string.Empty).ToString());


Can someone help me?

解决方案

There are two options.

Option1

The Expression property of DataColumn explained here http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.90).aspx[^] can be used to establish the relation of Total = Quantity * Price for the Total column as shown below, in which case the Total field will be a read only field.

DataTable products = new DataTable("Products");
products.Columns.Add("IdProduct",typeof(int),null);
products.Columns.Add("Quantity",typeof(double),null);
products.Columns.Add("Price",typeof(double),null);
products.Columns.Add("Total",typeof(double),"Price * Quantity");

products.Rows.Add(1, 5, 25.5);
products.Rows.Add(2,10,37.7);


float TotalOrder = float.Parse(products.Compute("sum(Total)",string.Empty).ToString());
Console.WriteLine (TotalOrder);
/* Output
Contents of products Table
Id Quantity Price Total 
1    5   25.5   127.5 
2   10   37.7   377 
3   15   63.2   504.5 

TotalOrder =
504.5
*/



Option 2

LINQ can be used as follows to calculate Total for each Row

DataTable products = new DataTable("Products");
products.Columns.Add("IdProduct",typeof(int),null);
products.Columns.Add("Quantity",typeof(double),null);
products.Columns.Add("Price",typeof(double),null);
products.Columns.Add("Total",typeof(double),null); 

products.Rows.Add(1, 5, 25.5);
products.Rows.Add(2,10,37.7);

//Count extension method is used to force immediate execution of the query
//as the LINQ query has deferred execution model and gets executed
//when it is iterated or a scalar function like Count is called.
products.AsEnumerable().Select ( p => { p.SetField<double>("Total",
     p.Field<double>("Quantity") * p.Field<double>("Price"));
     return p;
}).Count ();

float TotalOrder = float.Parse(products.Compute("sum(Total)",string.Empty).ToString());
Console.WriteLine (TotalOrder);
//Output will be same as shown above.


1. Why don't you just use Linq?

float total = (float) dt.AsEnumerable().Sum(r=>r.total);


2. try with capital S in sum.

dt.Compute("Sum(total)",string.Empty)



3. I'll put my smart-ass goggles and say that You should refactor your code...
(what if Compute returns null? why parsing approach?)


Try this

public object SumTotPremium;

//Calculation below 
SumTotPremium = datatable.Compute("Sum(Columname)", "");
//Show into Label
lblshowTotPremium.Text = 
String.Format("{0:INR #,##0.00}",Convert.ToDecimal(SumTotPremium));


这篇关于如何使用Compute计算总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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