Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,,

i am try to sum the value of total price to invoice web application. i have data base table with column price(represent the price to each item), i need to calculated the total of this column. i used sum property but an error appears :


Specified cast is not valid.

My Command:

C#
var Total = gen.InvoiceDetails.Sum(x => x.TotalPrice);


where gen.InvoiceDetails is the database context and table and TotalPrice is the column i want to sum


regards
Posted
Comments
Abhinav S 8-Jul-15 4:58am    
Is TotalPrice numeric?
Shambhoo kumar 8-Jul-15 6:16am    
what is your error message.??

Try this
var Total = gen.InvoiceDetails.AsEnumerable().Sum(x=>x.Field<int>("TotalPrice"));


Give the proper DataType for TotalPrice . here i take as Integer
 
Share this answer
 
v3
Comments
Badour alsamaraie 8-Jul-15 5:06am    
i use it but an error appears in x.Field<int> it is an exceptable!!!

what the Field<int>("TotalPrice") means???
Animesh Datta 8-Jul-15 5:08am    
what error came ?
Badour alsamaraie 8-Jul-15 5:35am    
'SalesINvoice.InvoiceDetail' does not contain a definition for 'Field' and no extension method 'Field' accepting a first argument of type 'SalesINvoice.InvoiceDetail' could be found (are you missing a using directive or an assembly reference?)
Animesh Datta 8-Jul-15 5:56am    
I modify the answer . check
you can try below code.
C#
var Total = gen.InvoiceDetails.ToList().Sum(x => x.TotalPrice);

if any issue write your comment.
 
Share this answer
 
Comments
Badour alsamaraie 8-Jul-15 6:35am    
okay this is work , but if i want to sum all price for special invoice (i mean can i use condition to select special price)?
Shambhoo kumar 8-Jul-15 6:44am    
yes you can do it too.

var Total = gen.InvoiceDetails.ToList().where(x=> x.columnName == specialInvoiceID).Sum(x => x.TotalPrice);

May it will help you. if it works then upvote answer and mark as accepted solution, mey it will also help other too :)
Badour alsamaraie 8-Jul-15 6:57am    
i recive error :

Specified cast is not valid
Shambhoo kumar 8-Jul-15 6:58am    
Use it.

var Total = gen.InvoiceDetails.ToList().where(x=> x.columnName == specialInvoiceID).AsEnumerable().Sum(x => x.TotalPrice);
Badour alsamaraie 8-Jul-15 7:01am    
the same error :
Specified cast is not valid
please clarify your question,
If,I'm right,Hope this helps you.



var Total= gen.InvoiceDetails.Select(x=>new InvoiceDetail{
TotalPrice=x.Field<int>("TotalPrice")
}).FirstOrDefault();
 
Share this answer
 
Comments
Badour alsamaraie 9-Jul-15 3:27am    
i want to sum all prices related to specific invoice number
hi
please go through the following link in which i performed sum operation using linq with array, linq with list and linq to sql.

http://tutorial.techaltum.com/Aggregate-Function-in-LINQ.html[^]
 
Share this answer
 
Comments
Badour alsamaraie 11-Jul-15 5:09am    
i am tried to convert list to array or use database summation the same result, i received message "Specified cast is not valid."
Member 10544672 19-Jul-15 2:45am    
My dear friend,please get the specific cast type from your datatable.

While we using code,suppose we have datatable

var dataType=dt.Column[4].DataType; //Please get column index from table

and cast type as you have required.

Thanks

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