Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two datatables and i need to compare two datatables values then getting the reult.
Case1:In both datatable values are not matching then merge two datatable results into Datatable3.

Datatable 1				Datatable2	
ID   Column2			ID     Column2
1	Test1                3      Test3
2   Test2                4      Test4

Here we need to compare two datatble ID's then if both are not matching then we need to merge the results like below
Datatable3 
ID  Column2
1    Test1
2    Test2
3    Test3
4    Test4


Case2:
If any value is matching those record not inserted into datatable.The result like below Datatable3.
Datatable 1				Datatable2	
ID   Column2			ID     Column2
1	Test1                3      Test3
2   Test2                5       Test5
3   Test3
4   Test4 

Datatable3
ID   Column2
1     Test1
2     Test2 
3     Test3
4     Test4
5      Test5 


Any one could you please help me.

What I have tried:

I have tried so many scenarios but it's not worked for me.
Posted
Updated 27-Mar-23 3:19am
v2
Comments
PIEBALDconsult 27-Mar-23 9:21am    
Seek ye the Master File Update algorithm.

Did a quick google search: c# compare two datatables[^] and the first result found: How to compare 2 dataTables[^] with several solutions to choose from.
 
Share this answer
 
That's complicated, because the standard Set operations don't include "Everything that isn't in both collections" as an option so you end up doing this:
dataYouWant = a.Except(b).Union(b.Except(a))
However, if you can get away from DataTables and use HashSets instead, there is the HashSet<T>.SymmetricExceptWith(IEnumerable<T>) Method (System.Collections.Generic) | Microsoft Learn[^] which will do what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900