I have two datatables and each has different 70-80 columns. I want to update the values of one datatable field with other datatable field values which are matching after comparing the primary keys of both in c#.
suppose I have one data table dt1 contains rows and 60 columns.
I have another datatable dt2 contains rows and 70 columns.
and 10 columns of dt1 matches with dt2. 30 rows of dt1 matches with dt2.
so I want to compare the primary key of both datatable and after if they match the update column values from dt1 to dt2.
Here is your answer, consider dt1=Customer and dt2=Order, CustomerId exist in both datatables, and i also joined them using LINQ extension method, and finally select columns from both datatables. Thanks.
static void Main(string[] args)
{
DataTable dt1 = new DataTable(); //dt1=Customer
dt1.Columns.Add("CustomerId", typeof(int));
dt1.Columns.Add("Name", typeof(string));
dt1.Rows.Add(1, "Customer A");
dt1.Rows.Add(2, "Customer B");
dt1.Rows.Add(3, "Customer C");
DataTable dt2 = new DataTable(); //dt2=Order
dt2.Columns.Add("OrderId", typeof(int));
dt2.Columns.Add("CustomerId", typeof(int)); //Fk
dt2.Columns.Add("OrderDate", typeof(DateTime));
dt2.Columns.Add("OrderAmount", typeof(double));
dt2.Rows.Add(1, 1,DateTime.Now,15000);
dt2.Rows.Add(2, 1, DateTime.Now,10000);
dt2.Rows.Add(3, 2, DateTime.Now,25000);
var result = dt2.AsEnumerable()
.Join(dt1.AsEnumerable(),
x => x.Field<int>("CustomerId"), //Order(CustomerId) FK
y => y.Field<int>("CustomerId"), //Customer Id(Pk)
(x, y) => new { dt2 = x, dt1 = y }) //dt2=Order, dt1=Customer
.Select(x => new
{
OrderId = x.dt2.Field<int>("OrderId"),
CustomerId = x.dt1.Field<int>("CustomerId"),
Name = x.dt1.Field<string>("Name"),
OrderDate = x.dt2.Field<DateTime>("OrderDate"),
OrderAmount = x.dt2.Field<double>("OrderAmount"),
});
foreach (var item in result)
{
Console.WriteLine("Order Id: {0}, CustomerId: {1}, Name: {2}, OrderDate: {3}, OrderAmount: {4}",
item.OrderId,item.CustomerId,item.Name,item.OrderDate,item.OrderAmount);
}
Console.ReadKey();
}
You will need to reference the tables individually unfortunately.
UPDATE tbl1 SET
col2c=(SELECT colc2 FROM tbl2 WHERE tbl2_pk=tbl1.tbl1_pk),
col4e=(SELECT cole4 FROM tbl2 WHERE tbl2_pk=tbl1.tbl1_pk)
WHERE test.tbl1.tbl1_pk=(SELECT tbl2_pk FROM tbl2 WHERE tbl1_pk=tbl2_pk)
Link to see results in table pre and post execution
Related
I am getting dynamic data from two sources. The column names are same but have different casing. While merging data tables it will create duplicate columns. I want to merge with same columns, regardless of the casing.
Code:
dataTable1.merge(dataTable2);
My column names in dataTable1 are:
Title
filePath
My column names in dataTable2 are:
title
FilePath
It should consider these two columns as identical.
MyCode:
public void TestOnMergeDataTables()
{
DataTable dt = new DataTable("Order");
DataColumn dc = dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(String));
dt.Rows.Add(1, "pramod");
dt.Rows.Add(2, "ravi");
dt.Rows.Add(3, "deepak");
dt.Rows.Add(4, "kiran");
dt.Rows.Add(5, "madhu");
DataTable dt2 = new DataTable("Order");
DataColumn dc2 = dt2.Columns.Add("ID", typeof(int));
dt2.Columns.Add("name", typeof(String));
dt2.Columns.Add("Type", typeof(String));
dt2.Rows.Add(6, "ashu", "Gen");
dt2.Rows.Add(7, "rudra", "Gen");
dt2.Rows.Add(8, "kavita", "Gen");
dt2.Rows.Add(9, "suman", "Gen");
dt2.Rows.Add(10, "lakshman", "Gen");
dt.Merge(dt2,true);
}
Result:
Output of the program
it should treat Name , name columns as same column
A simple way can be using linq. (This won't remove duplicates present in the columns). For removing duplicates, you have to use GroupBy.
var mergedTable = dataTable1.AsEnumerable()
.Select(row => new { Key = dataRow["Title"], Row = dataRow })
.Union(dataTable1.AsEnumerable()
.Select(row => new { Key = dataRow["title"], Row = dataRow }))
.CopyToDataTable();
how to add two datatables?
(dt1=name,phone_number)
and
(dt2=address,cityname)
Result:
(dt3=name,phone_number,address,cityname)
Ruben, you will need some primary key in order to combine both tables unless you are 100% sure that both data tables have the same number of rows and they will always be stored in the correct order.
There is a similar question answered and the link is: Combining n DataTables into a Single DataTable
Here goes the content of the provided link:
"The table has repeating primary keys after the Merge because no primary-key was defined. So either specify the PK or try this method here which i've written from scratch(so it's not really tested):
public static DataTable MergeAll(this IList<DataTable> tables, String primaryKeyColumn)
{
if (!tables.Any())
throw new ArgumentException("Tables must not be empty", "tables");
if(primaryKeyColumn != null)
foreach(DataTable t in tables)
if(!t.Columns.Contains(primaryKeyColumn))
throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");
if(tables.Count == 1)
return tables[0];
DataTable table = new DataTable("TblUnion");
table.BeginLoadData(); // Turns off notifications, index maintenance, and constraints while loading data
foreach (DataTable t in tables)
{
table.Merge(t); // same as table.Merge(t, false, MissingSchemaAction.Add);
}
table.EndLoadData();
if (primaryKeyColumn != null)
{
// since we might have no real primary keys defined, the rows now might have repeating fields
// so now we're going to "join" these rows ...
var pkGroups = table.AsEnumerable()
.GroupBy(r => r[primaryKeyColumn]);
var dupGroups = pkGroups.Where(g => g.Count() > 1);
foreach (var grpDup in dupGroups)
{
// use first row and modify it
DataRow firstRow = grpDup.First();
foreach (DataColumn c in table.Columns)
{
if (firstRow.IsNull(c))
{
DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
if (firstNotNullRow != null)
firstRow[c] = firstNotNullRow[c];
}
}
// remove all but first row
var rowsToRemove = grpDup.Skip(1);
foreach(DataRow rowToRemove in rowsToRemove)
table.Rows.Remove(rowToRemove);
}
}
return table;
}
You can call it in this way:
var tables = new[] { tblA, tblB, tblC };
DataTable TblUnion = tables.MergeAll("c1");
Used this sample data:
var tblA = new DataTable();
tblA.Columns.Add("c1", typeof(int));
tblA.Columns.Add("c2", typeof(int));
tblA.Columns.Add("c3", typeof(string));
tblA.Columns.Add("c4", typeof(char));
var tblB = new DataTable();
tblB.Columns.Add("c1", typeof(int));
tblB.Columns.Add("c5", typeof(int));
tblB.Columns.Add("c6", typeof(string));
tblB.Columns.Add("c7", typeof(char));
var tblC = new DataTable();
tblC.Columns.Add("c1", typeof(int));
tblC.Columns.Add("c8", typeof(int));
tblC.Columns.Add("c9", typeof(string));
tblC.Columns.Add("c10", typeof(char));
tblA.Rows.Add(1, 8500, "abc", 'A');
tblA.Rows.Add(2, 950, "cde", 'B');
tblA.Rows.Add(3, 150, "efg", 'C');
tblA.Rows.Add(4, 850, "ghi", 'D');
tblA.Rows.Add(5, 50, "ijk", 'E');
tblB.Rows.Add(1, 7500, "klm", 'F');
tblB.Rows.Add(2, 900, "mno", 'G');
tblB.Rows.Add(3, 150, "opq", 'H');
tblB.Rows.Add(4, 850, "qrs", 'I');
tblB.Rows.Add(5, 50, "stu", 'J');
tblC.Rows.Add(1, 7500, "uvw", 'K');
tblC.Rows.Add(2, 900, "wxy", 'L');
tblC.Rows.Add(3, 150, "yza", 'M');
tblC.Rows.Add(4, 850, "ABC", 'N');
tblC.Rows.Add(5, 50, "CDE", 'O');
After DataTable.Merge in MergeAll:
After some modifications to join the rows in MergeAll:
Update
Since this question arose in one of the comments, if the only relation between two tables is the index of a DataRow in the table and you want to merge both tables according to the index:
public static DataTable MergeTablesByIndex(DataTable t1, DataTable t2)
{
if (t1 == null || t2 == null) throw new ArgumentNullException("t1 or t2", "Both tables must not be null");
DataTable t3 = t1.Clone(); // first add columns from table1
foreach (DataColumn col in t2.Columns)
{
string newColumnName = col.ColumnName;
int colNum = 1;
while (t3.Columns.Contains(newColumnName))
{
newColumnName = string.Format("{0}_{1}", col.ColumnName, ++colNum);
}
t3.Columns.Add(newColumnName, col.DataType);
}
var mergedRows = t1.AsEnumerable().Zip(t2.AsEnumerable(),
(r1, r2) => r1.ItemArray.Concat(r2.ItemArray).ToArray());
foreach (object[] rowFields in mergedRows)
t3.Rows.Add(rowFields);
return t3;
}
Sample:
var dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("Name", typeof(string));
dt1.Rows.Add(1, "Jon");
var dt2 = new DataTable();
dt2.Columns.Add("Country", typeof(string));
dt2.Rows.Add("US");
var dtMerged = MergeTablesByIndex(dt1, dt2);
The result table contains three columns ID,Name,Country and a single row: 1 Jon US"
Source: Combining n DataTables into a Single DataTable
Last Access: 04-August-2016
First you can filter right column in to separate data datable
DataTable filterDatatable1= new DataTable();
DataTable filterDatatable2= new DataTable();
DataTable filterDatatable1= OriginaDatatable1.DefaultView.ToTable(false, "ColumnName1", "ColimnName2");
DataTable filterDatatable1= OriginaDatatable1.DefaultView.ToTable(false, "ColumnName1", "ColimnName2", "ColimnName3");
Then you can use merge function
DataTable dtAll = new DataTable();
dtAll.Merge(filterDatatable1);
dtAll.Merge(filterDatatable2);
I have two datatable dt1 and st2.
dt1 consists of PorductId ProductName FilePath:
1 Product1 c:\
2 Product2 c:\
3 Product3 c:\
4 Product4 c:\
dt2 consists of ProductName DateofDelivery:
Product2 2016-01-03
Product3 2016-03-02
Product5 2016-02-03
Product7 2014-09-01
I need to return all rows from dt2 where the ProductName of dt2 is in dt1 the result should be:
Product2 2016-01-03
Product3 2016-03-02
I've tried this, but its not working:
var matched = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable()
on table1.Field<string>("ProductName") equals table2.Field<string>("ProductName")
Really what you want to do is filter the second Datatable by the first, I would use a where instead of a join, the example below should replicate what your trying to do:
//Assemble the DataTables mentioned in your question
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("ProductName", typeof(string));
dt1.Columns.Add("Path", typeof(string));
dt1.Rows.Add(1, "Product1", "c:\\");
dt1.Rows.Add(2, "Product2", "c:\\");
dt1.Rows.Add(3, "Product3", "c:\\");
dt1.Rows.Add(4, "Product4", "c:\\");
DataTable dt2 = new DataTable();
dt2.Columns.Add("ProductName", typeof(string));
dt2.Columns.Add("Date", typeof(string));
dt2.Rows.Add("Product2", "2016-01-03");
dt2.Rows.Add("Product3", "2016-01-04");
dt2.Rows.Add("Product5", "2016-01-05");
dt2.Rows.Add("Product7", "2016-01-06");
//Get the values from dt1 to filter by
var filter = dt1.AsEnumerable().Select(b => b.Field<string>("ProductName")).ToList();
//Then filter the second list by the ProductName of the first list
var matched = dt2.AsEnumerable().Where(a => filter.Contains(a.Field<string>("ProductName")))
.ToList();
Hope that helps
try test this code
var dtproducts = dt1.Rows.Select(x => [ProductName]).ToArray();
var matches = (from System.Data.DataRow product in st1.Rows
where dtproducts.Contains(product["ProductName"])
select product).ToList();
I have written a program to compare two datatables with unique ids and create another datatable to insert certain columns which have the same id in common. I have demonstrated my requirement below.
These are the tables that needs to be compared:
And i need the output as below
but i receive an empty table as the result. I cannot understand where have i gone wrong. Could you please help me on this. I have provided my coding below.Please not that quantity and input are two datatables
DataTable result = new DataTable();
result.Columns.AddRange(new DataColumn[2] { new DataColumn("id"), new DataColumn("qty") });
foreach (DataRow row1 in input.Rows)
{
foreach (DataRow row2 in quantity.Rows)
{
if (row1["id"].ToString() == row2["id"].ToString())
{
result.ImportRow(row2);
}
else
{
result.ImportRow(row1);
}
}
}
return result;
You need a Left Join of 2 Data tables.
DataTable dtinput = new DataTable();
DataTable dtquantity = new DataTable();
dtinput.Columns.Add("id",typeof(int));
dtinput.Rows.Add("2");
dtinput.Rows.Add("4");
dtinput.Rows.Add("7");
dtquantity.Columns.Add("id", typeof(int));
dtquantity.Columns.Add("qty", typeof(int));
dtquantity.Rows.Add("1", "12");
dtquantity.Rows.Add("2", "13");
dtquantity.Rows.Add("3", "5");
dtquantity.Rows.Add("4", "6");
dtquantity.Rows.Add("7", null);
var results = from table1 in dtinput.AsEnumerable()
join table2 in dtquantity.AsEnumerable()
on (int)table1["id"] equals (int)table2["id"]
into outer
from row in outer.DefaultIfEmpty<DataRow>()
select row;
DataTable dt = results.CopyToDataTable();
This diagram should help you in future:
Try something like this:
var result = input.Rows.Where(x => quantity.Rows.Amy(y => x == y));
I hope this helps!
All, there are some question on this, but I can't seem to extract enough information to solve the problem for my case. I extract an unknown number of tables into SQL Server 'Tab1', 'Tab2', 'Tab3', ... , 'TabN'. The columns in these tables are different, but the row definitions are the same. I need to pull all the data in from the Server into N DataTables and then combine these to form a single DataTable. What I do currently is
int nTmpVolTabIdx = 1;
strSqlTmp = String.Empty;
using (DataTable dataTableALL = new DataTable())
{
while (true)
{
string strTmpVolName = String.Format("Tab{0}", nTmpVolTabIdx);
strSqlTmp = String.Format("SELECT * FROM [{0}];", strTmpVolName);
// Pull the data from 'VolX' into a local DataTable.
using (DataTable dataTable = UtilsDB.DTFromDB(conn, strTmpVolName, strSqlTmp, false))
{
if (dataTable == null)
break;
else
dataTableALL.Merge(dataTable);
}
nTmpVolTabIdx++;
}
...
}
This merges the DataTables but they are miss-aligned (padding blank cells onto the appended data set). I could append the columns of the new DataTable via a loop; but is there an easier/nicer way to do this (perhaps using LINQ)?
Thanks for your time.
Edit. To provide the example data sets.
What I required is
The individual tables are
After the first Merge operation I have the following
Thanks again.
The table has repeating primary keys after the Merge because no primary-key was defined. So either specify the PK or try this method here which i've written from scratch(so it's not really tested):
public static DataTable MergeAll(this IList<DataTable> tables, String primaryKeyColumn)
{
if (!tables.Any())
throw new ArgumentException("Tables must not be empty", "tables");
if(primaryKeyColumn != null)
foreach(DataTable t in tables)
if(!t.Columns.Contains(primaryKeyColumn))
throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");
if(tables.Count == 1)
return tables[0];
DataTable table = new DataTable("TblUnion");
table.BeginLoadData(); // Turns off notifications, index maintenance, and constraints while loading data
foreach (DataTable t in tables)
{
table.Merge(t); // same as table.Merge(t, false, MissingSchemaAction.Add);
}
table.EndLoadData();
if (primaryKeyColumn != null)
{
// since we might have no real primary keys defined, the rows now might have repeating fields
// so now we're going to "join" these rows ...
var pkGroups = table.AsEnumerable()
.GroupBy(r => r[primaryKeyColumn]);
var dupGroups = pkGroups.Where(g => g.Count() > 1);
foreach (var grpDup in dupGroups)
{
// use first row and modify it
DataRow firstRow = grpDup.First();
foreach (DataColumn c in table.Columns)
{
if (firstRow.IsNull(c))
{
DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
if (firstNotNullRow != null)
firstRow[c] = firstNotNullRow[c];
}
}
// remove all but first row
var rowsToRemove = grpDup.Skip(1);
foreach(DataRow rowToRemove in rowsToRemove)
table.Rows.Remove(rowToRemove);
}
}
return table;
}
You can call it in this way:
var tables = new[] { tblA, tblB, tblC };
DataTable TblUnion = tables.MergeAll("c1");
Used this sample data:
var tblA = new DataTable();
tblA.Columns.Add("c1", typeof(int));
tblA.Columns.Add("c2", typeof(int));
tblA.Columns.Add("c3", typeof(string));
tblA.Columns.Add("c4", typeof(char));
var tblB = new DataTable();
tblB.Columns.Add("c1", typeof(int));
tblB.Columns.Add("c5", typeof(int));
tblB.Columns.Add("c6", typeof(string));
tblB.Columns.Add("c7", typeof(char));
var tblC = new DataTable();
tblC.Columns.Add("c1", typeof(int));
tblC.Columns.Add("c8", typeof(int));
tblC.Columns.Add("c9", typeof(string));
tblC.Columns.Add("c10", typeof(char));
tblA.Rows.Add(1, 8500, "abc", 'A');
tblA.Rows.Add(2, 950, "cde", 'B');
tblA.Rows.Add(3, 150, "efg", 'C');
tblA.Rows.Add(4, 850, "ghi", 'D');
tblA.Rows.Add(5, 50, "ijk", 'E');
tblB.Rows.Add(1, 7500, "klm", 'F');
tblB.Rows.Add(2, 900, "mno", 'G');
tblB.Rows.Add(3, 150, "opq", 'H');
tblB.Rows.Add(4, 850, "qrs", 'I');
tblB.Rows.Add(5, 50, "stu", 'J');
tblC.Rows.Add(1, 7500, "uvw", 'K');
tblC.Rows.Add(2, 900, "wxy", 'L');
tblC.Rows.Add(3, 150, "yza", 'M');
tblC.Rows.Add(4, 850, "ABC", 'N');
tblC.Rows.Add(5, 50, "CDE", 'O');
After DataTable.Merge in MergeAll:
After some modifications to join the rows in MergeAll:
Update
Since this question arose in one of the comments, if the only relation between two tables is the index of a DataRow in the table and you want to merge both tables according to the index:
public static DataTable MergeTablesByIndex(DataTable t1, DataTable t2)
{
if (t1 == null || t2 == null) throw new ArgumentNullException("t1 or t2", "Both tables must not be null");
DataTable t3 = t1.Clone(); // first add columns from table1
foreach (DataColumn col in t2.Columns)
{
string newColumnName = col.ColumnName;
int colNum = 1;
while (t3.Columns.Contains(newColumnName))
{
newColumnName = string.Format("{0}_{1}", col.ColumnName, ++colNum);
}
t3.Columns.Add(newColumnName, col.DataType);
}
var mergedRows = t1.AsEnumerable().Zip(t2.AsEnumerable(),
(r1, r2) => r1.ItemArray.Concat(r2.ItemArray).ToArray());
foreach (object[] rowFields in mergedRows)
t3.Rows.Add(rowFields);
return t3;
}
Sample:
var dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("Name", typeof(string));
dt1.Rows.Add(1, "Jon");
var dt2 = new DataTable();
dt2.Columns.Add("Country", typeof(string));
dt2.Rows.Add("US");
var dtMerged = MergeTablesByIndex(dt1, dt2);
The result table contains three columns ID,Name,Country and a single row: 1 Jon US