locked
how to compare two datatables with particular coulmn and import matched rows in new datatable? RRS feed

  • Question

  • User1776267435 posted

    Hi, i have two tables below and i want to import matched rows into new datatable.

    DataTable dt1 = new DataTable();
    DataTable dt2 = new DataTable();
    DataTable dt3 = new DataTable();
    
    dt1.Columns.Add("Venid", typeof(string));
    dt1.Columns.Add("Name", typeof(string));
    dt1.Columns.Add("Branch", typeof(string));
    
    dt2.Columns.Add("Venid", typeof(string));
    
    dt3.Columns.Add("Venid", typeof(string));
    dt3.Columns.Add("Name", typeof(string));
    dt3.Columns.Add("Branch", typeof(string));
    
    DataRow dr;
    
    dr = dt1.NewRow();
    dr["Venid"] = "1001";
    dr["Name"] = "Hari";
    dr["Branch"] = "IT";
    dt1.Rows.Add(dr);
    
    dr = dt1.NewRow();
    dr["Venid"] = "1002";
    dr["Name"] = "Prasath";
    dr["Branch"] = "IT";
    dt1.Rows.Add(dr);
    
    dr = dt1.NewRow();
    dr["Venid"] = "1003";
    dr["Name"] = "HariPrasath";
    dr["Branch"] = "IT";
    dt1.Rows.Add(dr);
    
    dr = dt2.NewRow();
    dr["Venid"] = "1001";
    dt2.Rows.Add(dr);

    i need only matched row in dt3. i.e.

    VenId Name Branch

    1001  Hari     IT

    Thanks in Advance

    Thursday, April 4, 2013 5:45 AM

Answers

  • User1293050055 posted
    dt3=dt1.Clone();
     var matchRecord = from t1 in dt1.AsEnumerable()
                                  from t2 in dt2.AsEnumerable()
                                  where t1.Field<string>("Venid") == t2.Field<string>("Venid")
                                  select new { t1 };
                foreach (var r in matchRecord)
                {
                    var row = dt3.NewRow();
                    row["Venid"] = r.t1.Field<string>("Venid").ToString();
                    row["Name"] = r.t1.Field<string>("Name").ToString();
                    row["Branch"] = r.t1.Field<string>("Branch").ToString();
                    dt3.Rows.Add(row);
                }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 4, 2013 6:37 AM

All replies

  • User1293050055 posted
    dt3=dt1.Clone();
     var matchRecord = from t1 in dt1.AsEnumerable()
                                  from t2 in dt2.AsEnumerable()
                                  where t1.Field<string>("Venid") == t2.Field<string>("Venid")
                                  select new { t1 };
                foreach (var r in matchRecord)
                {
                    var row = dt3.NewRow();
                    row["Venid"] = r.t1.Field<string>("Venid").ToString();
                    row["Name"] = r.t1.Field<string>("Name").ToString();
                    row["Branch"] = r.t1.Field<string>("Branch").ToString();
                    dt3.Rows.Add(row);
                }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 4, 2013 6:37 AM
  • User1776267435 posted

    Super. Thanks aaa_78600.

    Could you please explain ur code.

    Thursday, April 4, 2013 7:17 AM
  • User1293050055 posted

    i think you don't know Linq. learn Linq.

    i joined both table and added record in third table.

     

    Friday, April 19, 2013 1:48 AM