Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have datatable that contain one int column
like name, surname, age(int)
now i filtering the using linq and assing all filter date to datatable
now i want to make sum of age
it will ginving error like Invalid usage of aggregate function Sum() and Type: String

ex:
C#
dttemp = dtItemsQty.AsEnumerable().Where(a => Convert.ToString(a["age"]) == lblage.Text ).CopyToDataTable();
string Total = Convert.ToString(dttemp.Compute("SUM(age)", String.Empty));
Posted
Updated 11-Feb-13 0:20am
v3

1 solution

Try this:
C#
if(dttemp!=null){
   var sum = dttemp.AsEnumerable().Sum(dr => dr.Field<int>("age"));
   Label1.Text = sum.ToString();</int>
}

OR
object sum;
C#
if(dttemp!=null){
   sum= dttemp.Compute("Sum(age)", "");
   Label1.Text = sum.ToString();

}

--Amit
 
Share this answer
 
v2
Comments
madhu.gone 11-Feb-13 6:24am    
No Above Two Are Not working for me

Ofter filtering age column was in string datatype
_Amy 11-Feb-13 6:25am    
Check for the count/values in dttemp. I think the table dttemp doesn't contains any data.
madhu.gone 11-Feb-13 6:27am    
its having string datatype
_Amy 11-Feb-13 6:28am    
Then change the type string to int in linq query.
madhu.gone 11-Feb-13 8:14am    
yes i just made that only

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