在C#中比较和合并数据表

时间:2016-02-29 13:42:31

标签: c#

我有两个不同的数据表

datatable1: customer, product, shippedqty
datatable2: customer, product, febforecast, marforecast, aprforecast

此处客户和产品列相同

我需要在datatable1datatable2列上将customerproducts进行比较,我需要将结果集设置为新的datatable

customer,product,dhipedqty,febforecast,marforecast,aprforecast

这就是我尝试过的:

var tablesJoinend = from t1 in dt.Rows.Cast<DataRow>() 
                    join t2 in forecastdatatable.Rows.Cast<DataRow>() 
                    on new { A = t1["Customer"].ToString(), B = t1["ProductCode"].ToString() } 
                    equals new { A = t2["customer"].ToString(), B = t2["NDC"].ToString() } 
                    select t1;

现在我尝试了这个没有用的

var results = from t1 in dt.Rows.Cast<DataRow>()
                              join t2 in forecastdatatable.Rows.Cast<DataRow>() on new { A = t1["Customer"].ToString(), B = t1["ProductCode"].ToString() } equals new { A = t2["customer"].ToString(), B = t2["NDC"].ToString() }
                              select new
                              {
                                  CustID = Convert.ToInt32(t1["Customer"]),
                                  ColX = Convert.ToInt32(t1["ProductCode"]),
                                  ColY = Convert.ToInt32(t1[3]),
                                  a = Convert.ToInt32(t1[4]),
                                  xx = Convert.ToInt32(t2[4]),
                                    qq = Convert.ToInt32(t2[5])
                              };

0 个答案:

没有答案