1

I have two datatables as shown in screenshot (table 1 and table 2). I want to merge these tables based on the Name column and create the result table as you see ...

Can you please explain to me how to achieve this?

Note: my table is simple datatable in c# not SQL table Thanks

enter image description here

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
programmer
  • 29
  • 5
  • Think in terms of full outer join in databases. – Pawan Kumar Dec 02 '19 at 08:54
  • Is this all done only on C# or with a database? – Marco Salerno Dec 02 '19 at 09:20
  • @programmer The question is unclear and obviously confused people. The two answers talk about *completely* different things. One could also mention [DataTable.Merge](https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.merge?view=netframework-4.8#System_Data_DataTable_Merge_System_Data_DataTable_System_Boolean_System_Data_MissingSchemaAction_) which can already merge one DataTable into another. What do *you* mean by DataTable or Merge though? `DataTable` is *never* used for database tables for example. Are you using the term incorrectly? Or do you really mean ADO.NET data tables – Panagiotis Kanavos Dec 02 '19 at 09:25
  • @programmer the `datatable` tag description says it's ambiguous and a more specific one should be used. Which data tables are you talking about, what are you trying to do, what did you try? Where does `LINQ` come in? – Panagiotis Kanavos Dec 02 '19 at 09:27
  • are you merging databases or tables? as pretty much only thing that would have called those databases would be a dbase variant.. what type of "database" is holding these tables – BugFinder Dec 02 '19 at 09:27
  • my table is simple datatable in c# not SQL table Thanks – programmer Dec 02 '19 at 09:36
  • @programmer use `DataTable.Merge`. Have you tried it? There are several similar questions, from those that just ask how to merge to those asking for more complex scenarios, like ignoring columns, handling changes, merging multiple tables etc – Panagiotis Kanavos Dec 02 '19 at 09:37

1 Answers1

1

This is an example about how to merge 2 DataTables:

DataTable table1 = new DataTable("Table 1");
table1.Columns.Add("Name", typeof(string));
table1.Columns.Add("Number A", typeof(int));
table1.Columns.Add("Comment", typeof(string));
table1.Columns.Add("ID", typeof(int));
table1.Rows.Add("John", 25800, "text", 12);

DataTable table2 = new DataTable("Table 1");
table2.Columns.Add("Name", typeof(string));
table2.Columns.Add("Number B", typeof(int));
table2.Columns.Add("Age", typeof(int));
table2.Columns.Add("City", typeof(string));
table2.Rows.Add("Rose", 92610, 24, "London");

table1.Merge(table2);

The result is the one you requested.

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • @PanagiotisKanavos sorry I don't understand your question – Marco Salerno Dec 02 '19 at 09:33
  • Oops, I thought you were adding the original columns to the target table. You should at least provide a link to `Merge` though - if someone has to ask about Merge, they probably don't know where to find it either – Panagiotis Kanavos Dec 02 '19 at 09:36
  • Here is the link: [DataTable.Merge](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable.merge). My small experience with the `DataTable.Merge` indicates that this method is not flexible enough though. – Theodor Zoulias Dec 02 '19 at 12:10