Compare Datatables

Hello guys,

I have two data tables with similar structure (Table A and Table B). I want to compare both the datatables with column name Unique and Table A should only have data which is not found in Table B. Please see examples below, Column1 - 326 and 60 in Table A should be the final output.

Table A
|Column 1 | Column 2 | Column 3 | Column 4 | Unique|

|— | — | — | — | —|

|220 | F | RON | 314 | 0000186461FMYR0055101688|
|199 | F | TND | 350 | 0000186461FMAD0055101684|
|395 | O | CNY | 349 | 0000186461FMXN0055101686|
|315 | F | WST | 15 | 0000186461FILS0055101681|
|326 | O | XAF | 497 | 0006335392FCAD0040455902|
|60 | O | FKP | 419 | 0006335392FCHF0040455903|

Table B

Column 1 Column 2 Column 3 Column 4 Unique
325 O CRC 270 0000186461FILS0055101681
190 O SEK 100 0000186461FINR0055101682
433 F ILS 360 0000186461FJPY0055101683
275 F IDR 358 0000186461FMAD0055101684
120 F COP 454 0000186461FMXN0055101686
298 F EUR 373 0000186461FMYR0055101688

Can someone help please

Hi @somesh.r.nagar ,

Use below LINQ Query hope it helps you

Filtered_DT as Datatable :

(From rowA In DT_1.AsEnumerable()
            Where Not DT_2.AsEnumerable().Any(Function(rowB) rowB.Field(Of String)("Unique") = rowA.Field(Of String)("Unique"))
            Select rowA).CopyToDataTable()

Regards,
Vinit Mhatre

@somesh.r.nagar

Can you try this once

dt1.AsEnumerable().ExceptBy(dt2.AsEnumerable().Select(Function(a) a("Unique").ToString),Function(b) b("Unique").tostring).CopyToDataTable

Final output should be only below, but i am getting multiple results with this linq query

|326 | O | XAF | 497 | 0006335392FCAD0040455902|
|60 | O | FKP | 419 | 0006335392FCHF0040455903|

Getting error

Compiler error(s) encountered processing expression “dt_1.AsEnumerable().ExceptBy(dt_2.AsEnumerable().Select(Function(a) a(“Unique”).ToString),Function(b) b(“Unique”).tostring).CopyToDataTable”.
‘ExceptBy’ is not a member of ‘System.Data.EnumerableRowCollection(Of System.Data.DataRow)’.

Hi,

Can you try the following sample?

arrDr = dtA.AsEnumerable.Where(Function(r) not dtB.AsEnumerable.Any(Function(r2) r("Unique").ToString=r2("Unique").ToString)).ToArray()

note: arrDr is DataRow array type

Sample
Sample20240328-1.zip (10.3 KB)

Regards,

1 Like

Thank you as always, solution is exactly as I required

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.