合并两个数据表并在c#中添加两列 [英] merge two datatable and add two column in c#

查看:82
本文介绍了合并两个数据表并在c#中添加两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个查询如何添加两个数据表列值并合并到另一个数据表中。

这里我将两个数据表值合并为一个但我添加两列时出现了类型错误。任何人都可以帮助我。







  var  ret = 来自 p   dfs.AsEnumerable()
join q dhs.AsEnumerable()on p.Field< string>( ItemName)等于q.Field< string>( ItemName into UP
from q UP.DefaultIfEmpty()
选择 new
{
ID = p.Field< string>( ItemName),
FS = p.Field< string>( TotalQtyFS ),
HS = q.Field< string>( TotalQtyHS),
数量= p.Field< string>( CurrentQty),
金额=( int ?)((p == null )? 0 :p [ TotalQtyFS] )+( int ?)((q == null )? 0 :q [ TotalQtyHS]),
// add = Convert.ToString(p.Field< Int32>(TotalQtyFS)+ q.Field< Int32>( TotalQtyHS)),

};







这里dfs和dhs是不同的数据表我合并成一个现在我想添加TotalQtyFS和TotalQtyHS。在这里,你看到上面的代码,我尝试了两种不同的类型,但我得到了类型错误。解决这个问题。

解决方案

Sinisa Hajnal指出我的解决方案及其评论< blockquote class =quote>

Quote:

另外,你确定Total Quality应该是字符串

而你的回复

引用:

并且它不是一个字符串它的int



如果你已经定义了列 TotalQtyFS TotalQtyHS CurrentQty 是整数,然后这些代码行将导致问题

 FS = p.Field< ; string>(TotalQtyFS),
HS = q.Field< string>(TotalQtyHS),
数量= p.Field< string>(CurrentQty),

他们应该读

 FS = p.Field<  int >(TotalQtyFS),
HS = q.Field< int >(TotalQtyHS),
数量= p.Field< int >(CurrentQty),


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.

解决方案

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"),


这篇关于合并两个数据表并在c#中添加两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆