Click here to Skip to main content
14,241,320 members
Rate this:
Please Sign up or sign in to vote.
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:

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.??
Rate this:
Please Sign up or sign in to vote.

Solution 1

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


Give the proper DataType for TotalPrice . here i take as Integer
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

you can try below code.
var Total = gen.InvoiceDetails.ToList().Sum(x => x.TotalPrice);

if any issue write your comment.
   
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
Shambhoo kumar 8-Jul-15 7:05am
   
check this.

var Total = gen.InvoiceDetails.ToList().where(x=> x.columnName == specialInvoiceID).AsEnumerable().select(x=> x.TotalPrice).Sum(x => x.TotalPrice);
Badour alsamaraie 9-Jul-15 3:25am
   
the same error :

Specified cast is not valid.
Shambhoo kumar 9-Jul-15 4:11am
   
use .ToList() before sum function.
Badour alsamaraie 9-Jul-15 4:21am
   
the same error .. actually i don't know whats the meaning of cast is not valid??
Rate this:
Please Sign up or sign in to vote.

Solution 3

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();
   
Comments
Badour alsamaraie 9-Jul-15 3:27am
   
i want to sum all prices related to specific invoice number
Rate this:
Please Sign up or sign in to vote.

Solution 4

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[^]
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100