Comparing Datatables (multiple columns)

Priya 24

New member
Joined
May 6, 2020
Messages
3
Programming Experience
5-10
Hi,

Using Linq I need to Compare two Datatable (Table1 -- Datatable1 and Table2 -- Datatable2) Result need to be populated in Table 3, How to Achieve this.?

Need to Compare UserID,EmpName and Age 3 colums from Table 1 with Table 2 and Result need to be populated as Table3 using C#.net

1588761157763.png
 
It's not for us to write your code for you. If you MUST use LINQ then it's presumably homework, so it would be cheating, apart from anything else. You need to make an effort and then we can help you with issues you strike along the way. If you haven't tried then you haven't failed, so there's nothing for us to help with.

Apart from that, you say that you want to compare but you don't explain how. Don't expect us to guess what the conditions are from a single example. Explain what you're actually looking for.
 
C#:
var diffIDs = dt1.AsEnumerable().Select(r => r.Field<string>("UserId")).Except(dt2.AsEnumerable().Select(r => r.Field<string>("UserId")));
DataTable dt3 = (from row in dt1.AsEnumerable()
                    join name in diffIDs on row.Field<string>("UserId") equals name
                    select row).CopyToDataTable();

I tried this but i could not achieve as i need to compare three columns from datatable1 to 3 columns in datatable 2 and datatable must have unique records
 
Last edited by a moderator:
Please use code tags when posting to the forums. As you can see, this helps with the readability of the source. I've edited your code this time for you and included the code tags.
 
The main issue with your approach is that you are trying to "compare" the two tables, instead of trying to "merge" them together. Consider happens if you select the user id, name, and ages from the two tables, and then sort and select for unique user ids to generate your output.
 
Back
Top Bottom