Merging two DataTables in C#

17,499

try something like this, this is an example how to merge two datatables...

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Data; 

namespace ConsoleApplication1 
{ 
  class Program 
  { 
     static void Main(string[] args) 
     { 
          DataColumn col; 
          DataTable table1 = new DataTable(); 
          table1.PrimaryKey = new DataColumn[] { 
          col = table1.Columns.Add("slot_id") 
           }; 
          col.DataType = typeof(int); 
          col.Unique = true; 
          col = table1.Columns.Add("appointment_time"); 
          col = table1.Columns.Add("patient_name"); 
          col = table1.Columns.Add("patient_doctor"); 

          table1.Rows.Add(1, "0900", "George Michael"); 

          DataTable table2 = new DataTable(); 
          table2.PrimaryKey = new DataColumn[] { 
          col = table2.Columns.Add("slot_id") 
           }; 
          col.DataType = typeof(int); 
          col.Unique = true; 
         col = table2.Columns.Add("appointment_time"); 

          table2.Rows.Add(1, "0900"); 
          table2.Rows.Add(2, "1000"); 
          table2.Rows.Add(3, "1100"); 
          table2.Rows.Add(4, "1200"); 

         DataTable merged = new DataTable(); 
        merged.Merge(table1); 
        merged.Merge(table2); 

        foreach (DataColumn dc in merged.Columns) 
        Console.Write(dc.ColumnName + "\t"); 
        Console.WriteLine(); 

       foreach (DataRow dr in merged.Rows) 
       { 
            foreach (DataColumn dc in merged.Columns) 
            Console.Write(dr[dc.ColumnName] + "\t"); 
            Console.WriteLine(); 
       } 
       Console.WriteLine(); 

       Console.Write("Press any key to continue . . . "); 
       Console.ReadKey(true); 
       Console.WriteLine(); 
    } 
  } 
} 
Share:
17,499
Nithesh Narayanan
Author by

Nithesh Narayanan

I am Nithesh Adukkam Narayanan, a Full stack developer and Architect with 11+ years of experience in software development, design, best practices, and mentoring. My primary skillset in the .Net tech stack (C#, .net, .net core, Web API, MVC) along with the front end (React JS, JavaScript, Typescript, webpack). Been an innovator and played a key role in R&D projects using python, image processing, and ICR. Proven expertise in cutting-edge technologies like cloud (azure, aws), event-driven, micro-service architecture, micro front end architecture, Docker, and containers. Hands-on in CI/CD (Azure DevOps) and agile methodologies.

Updated on July 06, 2022

Comments

  • Nithesh Narayanan
    Nithesh Narayanan almost 2 years

    I Have two datatables as follows

    Table1
    --------------------------------
    Id     | Batch  | Qty
    -----------------------------
    1         A1       5
    2         A2       5
    3         A3       5
    4         A4       5
    
    
    Table2
    --------------------------------
    Id     | Batch  | Qty
    -----------------------------
    1         A1       6
    2         A2       6
    3         A3       6
    5         A5       10
    
    Expected result
    --------------------------------
    Id     | Batch  | Qty
    -----------------------------
    1         A1       6 (Qty updated)
    2         A2       6 (Qty updated)
    3         A3       6 (Qty updated)
    4         A4       5 (remains as same)
    5         A5       10 (row in table 2)
    

    How can i achieve this in c#. If anybody knows this data table operation please share..

    • Tim
      Tim over 12 years
      If there is a matching ID and Batch in both tables, do you want the higher quantity value, or just simply the value from table 2?
    • Nithesh Narayanan
      Nithesh Narayanan over 12 years
      @Tim:want to update the value from table2
    • xanatos
      xanatos over 12 years
      What version of C#? Are your using DataTable as in the class DataTable? Are you using the generic version? (as in Typed DataSet/DataTable)?