Click here to Skip to main content
14,699,491 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi to all,
I have a query how to add tow datatable column values and merge into another datatable.
Here i done merge of two datatable values into one but i got cast type error while adding two column. can anyone help me.



var ret = from p in dfs.AsEnumerable()
                         join q in dhs.AsEnumerable() on p.Field<string>("ItemName") equals q.Field<string>("ItemName") into UP
                         from q in UP.DefaultIfEmpty()
                         select new
                         {
                             ID = p.Field<string>("ItemName"),
                             FS = p.Field<string>("TotalQtyFS"),
                             HS = q.Field<string>("TotalQtyHS"),
                             Qty = p.Field<string>("CurrentQty"),
                             Amount = (int?)((p == null) ? 0 : p["TotalQtyFS"]) + (int?)((q == null) ? 0 : q["TotalQtyHS"]),
                           //  add = Convert.ToString(p.Field<Int32>("TotalQtyFS") + q.Field<Int32>("TotalQtyHS")),

                         };



here dfs and dhs are different datatable i merge into one now i want to add "TotalQtyFS" and "TotalQtyHS". here u see above code i tried two different type but i got cast type error.how to resolve this.
Posted
Comments
[no name] 3-Dec-15 3:54am
   
What is the exception you are getting?
   
specified cast is not valid
Sinisa Hajnal 3-Dec-15 6:48am
   
Are you sure your two q variables aren't colliding?
Also, are you sure Total Quality should be string
   
hmm yeah sure, and it not a string its int

1 solution

Sinisa Hajnal pointed me towards the solution with their comment
Quote:
Also, are you sure Total Quality should be string
and your response
Quote:
and it not a string its int

if you have defined the columns TotalQtyFS, TotalQtyHS and CurrentQty are integers then these lines of code will cause issues
FS = p.Field<string>("TotalQtyFS"),
HS = q.Field<string>("TotalQtyHS"),
Qty = p.Field<string>("CurrentQty"),
They should read
FS = p.Field<int>("TotalQtyFS"),
HS = q.Field<int>("TotalQtyHS"),
Qty = p.Field<int>("CurrentQty"),
   
Comments
JOTHI KUMAR Member 10918227 4-Dec-15 23:43pm
   
got same error here i used like this:
FS = p.Field<int>("TotalQtyFS"),
HS = q.Field<int>("TotalQtyHS"),
Qty = p.Field<int>("CurrentQty"),
add = p.Field<int>("TotalQtyFS") + q.Field<int>("TotalQtyHS"),
in middle of field i used "int"
CHill60 8-Dec-15 10:05am
   
That won't even compile as the field type cannot be inferred, nor is it what I suggested in my solution
The field types are being zapped by the editor. I'll "edit" your comment so that I can see what you are doing and have another look
CHill60 8-Dec-15 10:13am
   
I just pasted your code in and it worked fine for me - how are your source datatable columns defined?

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