Compare two datatables using LINQ

Hello Community

I am struggling to compare two datatables.
I have two datatables with thousands of rows. Theres a column “In Stock” in both of them which contains the values (eg. 2,4,10,etc.). I want to compare these two columns if they have the exact same values or not. Please help with the LINQ.

we can join data for checking it or some other modified approaches.

Referring to Column Number “In Stock” we would assume that you will check it for particular items identified an Identity (e.g. Look for employees with their EMPid, look for Invoices with its invoice no).

Feel free to give us more information on it. Also, sample data will be helpfully for us along the description of expected output

1 Like

Thanks. Sure
From google drive i am downloading a csv and uploading that csv to another portal.
I want to basically check if the CSV that has been uploaded on the portal same as that of the drive one, not the old one.

So, to check, i download that uploaded CSV from the portal and compare it with the one i downloaded from the drive. If its same then its all good. The datatable looks like this

Basically i have to match the “in stock” numbers because if the new file was not uploaded on the portal the “in stock” numbers would be different and the old ones.
And if it was uploaded they would be the same as that of the one i downloaded from the drive.

as far we have understood give a try on joining the 2 datatable on the column Product Code, then the different In Stock values can be compared

You can do it also with Join DataTable

When doing it with a LINQ we would suggest to specify the requirements more detailed. e.g.

Dt1 - new reported
Dt2 - old report, downloaded from portal

Check all rows from dt1 against dt2 where product Code is same and InStock is different.
If result is empty we have already updated the InStock, if different we do have to update

1 Like

Thankuou you gave some clarity…but can you explain the steps on how to go about it? which join should i take in join datatable and after that how should i check the values and everything. Really appreciate the help!

Join datatable Activity
give a start on Left Join - leftTable: dt1 RightTable: dt2
Join cols Product Code And InStock

1 Like

By this i am able to obtain some extra rows in the final DT. Those are the ones which did not match. But what if there are some extra rows in the second DT? Will i have to put it in the InputDataTable1 then check again?

just go through the provided link (Story Line) and explore it in detail.
When interested in dt2 rows also not present in dt1 we can use full Join.

But as mentioned above: It is recommended to start with clearly defined requirements defining the relevant scenarios. Then we can check for best fitting solution apporaches

1 Like

Yes got that… we would specify it as per our needs… i have joined both the columns…and got a datatable containing some extra rows…what condition can i put now to check and return a true or false values? I’m sorry im a bit new so a bit confused

just write down the clear requirement and we will help.

in general check the join datatable result on

left Product code is Null/empty - we only have this record in dt2
right Product code is Null/empty - we only have this record in dt1
left in Stock = right In Stock - same record
Left in Stock not equals Right In Stock - we have a modifed record

1 Like

This is the clear requirement:
The CSV which i will download from the portal will be only checked that whether it has been uploaded correctly or not. We will use the DT of the sheet from the drive for that comparison.
So we will take DT1 as the drive sheet and DT2 ad the Portal sheet and perform the join.

Then we have to check if there are any extra rows in DT1 which are not there in DT2. Both the datables should be exactly the same. If there are any extra rows (mismatched rows) we should get them in a separate datatable. What should be the exact steps.

Thankyou very much. I appreciate your help

Perfect, Join datatable full Join IS a good start for this

Thankyou veryy much. I think i got it now