Prashant Khadatkar
Prashant Khadatkar

Reputation: 200

Merge two datatable in to one datatable and remove duplicate as per single column

I have two datatable, say T1 and T2. Following are the columns in both table:

1) Filename,
2) Size,
3) PATH,
4) rank,
5) DocTitle,
6) HitCount

Now, I need to merge table T1 and T2 but entries should not get duplicated as per "Filename" column.

I cant user T1.Merge(T2) to do my work. Do anyone have suggestion on the same. Thanks.

Upvotes: 0

Views: 1665

Answers (2)

Artiom
Artiom

Reputation: 7837

if you need to implement this in C# than smth like that could work for you

var fileNames = t1.Rows.OfType<DataRow>().Select(row => row["FileName"]).ToList();
var rowsToAdd = t2.Rows.OfType<DataRow>().Where(row => !fileNames.Contains(row["FileName"])).ToList();

foreach (var dataRow in rowsToAdd)
{
    t1.ImportRow(dataRow);
}

but it's better to use SQL filters and after that to put in DataTable.

Upvotes: 1

Java_Alert
Java_Alert

Reputation: 1179

select * into table_name from (select * from T1  union select * from t2 )

NOTE

It will work only and only when both of table have same number of columns

Upvotes: 0

Related Questions