0

I have a two different Datatables

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

Here Customer and product columns are same

I need to compare datatable1 with datatable2 on customer and products columns and i need to get the result set into new datatable as

customer,product,dhipedqty,febforecast,marforecast,aprforecast

This is what i've tried:

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;

Now i have tried this which it was not worked

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])
                              };
user2181338
  • 71
  • 3
  • 10
  • var tablesJoinend = from t1 in dt.Rows.Cast() join t2 in forecastdatatable.Rows.Cast() on new { A = t1["Customer"].ToString(), B = t1["ProductCode"].ToString() } equals new { A = t2["customer"].ToString(), B = t2["NDC"].ToString() } select t1; – user2181338 Feb 29 '16 at 13:43
  • thant for the link but in that example they have compared only one column but i want to compare two columns at the same time – user2181338 Feb 29 '16 at 13:47
  • So you have tried something. But did it work? If not you should tell us what's wrong with it. As far as i can see you are selecting `t1` which are the rows of `dt` and not the forecast rows. But you want columns of both tables. So you could select an anonymous type that contains both rows. Then use a `foreach`-loop to fill your third table. – Tim Schmelter Feb 29 '16 at 13:48
  • can you please give me the loop how to start it so that it will help me – user2181338 Feb 29 '16 at 13:53

0 Answers0