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
127 views
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 };

dt1.Merge(dt2);

Result will come with saparate tag and value.

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

Related Questions

+1 vote
0 answers 91 views
+12 votes
1 answer 111 views
+8 votes
2 answers 106 views
+8 votes
1 answer 88 views
+5 votes
7 answers 14,590 views
+1 vote
1 answer 951 views
0 votes
1 answer 219 views
0 votes
0 answers 153 views
+2 votes
2 answers 446 views

Not a Member yet?

Ask to Folks Login

My Account
838 Folks are online
67 members and 771 guest online
Your feedback is highly appreciated