Hey there! any question in your mind? Ask It Now!.

Popular Categories

How to merge two tables without duplicate values for a column?

+6 votes
asked in Software Testing by mika (1,290 points)

I have two tables table1 and table2 and i want to merge these table for a column with no duplicates value

Now if I use table1.Merge() I get the rows with both default and actual values for tag abc. I need only the actual values for a particular tag present in 2nd table, if not the default values from 1st table. How do i do this?

1 Answer

+1 vote
answered by jazs (1,220 points)
selected by mika
Best answer

you can use this 

var dt1 = new DataTable();
var prime1 = dt1.Columns.Add("Tag", typeof(string));
dt1.Columns.Add("Value", typeof(string));
dt1.Rows.Add(new object[]{"abc", "default"});
dt1.Rows.Add(new object[]{"xyz", "default"});
dt1.PrimaryKey = new DataColumn[]{ prime1 };

var dt2 = new DataTable();
var prime2 = dt2.Columns.Add("Tag", typeof(string));
dt2.Columns.Add("Value", typeof(string));
dt2.Rows.Add(new object[]{"abc", "12"});
dt2.PrimaryKey = new DataColumn[]{ prime2 };


Result will come with saparate tag and value.

commented by mika (1,290 points)
Nice thanks!

Related Questions

+1 vote
0 answers 122 views
+12 votes
1 answer 177 views
+8 votes
2 answers 175 views
+8 votes
1 answer 184 views
+5 votes
7 answers 15,006 views
+1 vote
1 answer 1,245 views
+2 votes
2 answers 516 views
+1 vote
1 answer 2,384 views
+2 votes
1 answer 1,475 views

Not a Member yet?

Ask to Folks Login

My Account
432 Folks are online
0 members and 432 guest online
Your feedback is highly appreciated