Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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:
C#
float TotalOrder = float.Parse(dt.Compute("sum(total)",string.Empty).ToString());

Can someone help me?
Posted
Updated 11-Jun-12 22:49pm
v2
Comments
RDBurmon 13-Jun-12 9:35am    
Thanks Everyone who replied to this thread , So giatuan, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

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.
C#
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
C#
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.
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 12-Jun-12 5:58am    
Complete one +5!
VJ Reddy 12-Jun-12 6:59am    
Thank you, Prasad :)
Espen Harlinn 12-Jun-12 8:37am    
5'ed!
VJ Reddy 12-Jun-12 9:02am    
Thank you, Espen :)
Manas Bhardwaj 13-Jun-12 4:10am    
Nice +5 :)
1. Why don't you just use Linq?
C#
float total = (float) dt.AsEnumerable().Sum(r=>r.total);

2. try with capital S in sum.
C#
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?)
 
Share this answer
 
Try this

public object SumTotPremium;

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900