-2

I have 2 datatables

1) which runs a query and stores the values

Select customer, address,**zipcode** from sometable where zipcode = ? 
mycmd.Parameters.Add("@zipcode", OdbcType.VarChar).Value = {**how to bring the value from datatable 2 zip**;

into datatable?

2) which reads a csv file into datatable zip,lat,long

I am running a foreach loop on datarows from datatable 1 and

If rows was found match zip values in the where condition I then want to include the lat and long values from the datatable 2

My end result will look like customer, address,zipcode,lat,long.

I searched several websites online but was not able to find any thing.

I can't post the two data table code in here, can I?

halfer
  • 19,824
  • 17
  • 99
  • 186
CodeMan
  • 133
  • 3
  • 19
  • Not sure if this helps you http://stackoverflow.com/questions/665754/inner-join-of-datatables-in-c-sharp http://stackoverflow.com/questions/20760681/linq-join-two-datatables – Vinu Dec 22 '16 at 02:22
  • `I can't post the two data table code in here, can I?` - presumably you could add the names of your tables and the columns they have. I assume one of them is called `zipcode` and the other one is called `sometable` (but you might as well give it its real name). I assume you are wanting to do a join, but since the structure of the two input tables is not clear, it is presently not possible to tell you what column is common to both of them. Can you add the necessary extra detail? – halfer Dec 22 '16 at 13:51

1 Answers1

0

This fixed my issue

public static DataTable MergeTwoDataTables(DataTable dt1, DataTable dt2)
        {

 var collection = from t1 in dt1.AsEnumerable()
 join t2 in dt2.AsEnumerable() on ToNumericOnly(t1["ZIPCODE"].ToString().Trim().Substring(0,5))equals t2["Zip"] into DataGroup
 from item in DataGroup.DefaultIfEmpty()                            
  select new
   {
      CUSTOMER = ToNumericOnly(t1["CUSTOMER"].ToString()),
      ADDRESS=t1["ADDRESS"],
      ZIPCODE = t1["ZIPCODE"].ToString().Trim(),
      LAT= item == null ? string.Empty : item["lat"],
      LONG= item == null ? string.Empty : item["long"]
       };
 DataTable result = new DataTable("CustomerInfo");
   result.Columns.Add("CUSTOMER", typeof(string));
   result.Columns.Add("ADDRESS", typeof(string));         
   result.Columns.Add("ZIPCODE", typeof(string));
   result.Columns.Add("LAT", typeof(string));
   result.Columns.Add("LONG", typeof(string));

foreach (var item in collection)
   {
    result.Rows.Add(item.CUSTOMER,item.ADDRESS,item.ZIPCODE,
item.LAT,item.LONG);
 Console.WriteLine("{0}", item);
  //Console.ReadLine();
            }
            return result;
        }
CodeMan
  • 133
  • 3
  • 19