Is it possible? Datatable matching duplicates using linq


is it possible to have this result using linq on comparing 2 datatables? It’s easy using for loops but i want to do it via linq. TIA!

Hey!

Try this

Assign NewDt = Dt.DefaultView.ToTable(True)

This will remove the duplicates in entire table

Assign NewDt = Dt.DefaultView.ToTable(True,"Col1","Col2")

The above expression will remove the duplicates in particular column

You can use the first expression to remove the duplicates in entire table

Regards,
NaNi

1 Like

Hi,

Can you try the following sample?

dtResult = dt1.AsEnumerable.GroupBy(Function(r) r("ID").ToString).SelectMany(Function(g) g.Select(Function(r1,i) dtResult.LoadDataRow({r1("ID"),r1("Name"),r1("LastName"), if(dt2.AsEnumerable.Where(Function(r2) r2("ID").ToString=g.key).Count>i , dt2.AsEnumerable.Where(Function(r2) r2("ID").ToString=g.key)(i).item("Age").ToString,"N/A")},false))).CopyToDataTable

Sample20220726-4.zip (2.9 KB)

Regards,

1 Like

Thank you for this it works perfectly!

1 Like

Hello, I’m having a hard time implementing your code to my correct columns can you help me with this?

Hi,

Can you share your workbook as a file?

Regards,

dt1
REVERSAL_INSTAPAY_SENDING_MC.xlsx (18.7 KB)

dt2
02132022_INSTAPAY_SENDING_MC.xlsx (19.0 KB)

thank you so much!

dt_result

Hi,

Which column should we add or update from dt2 to dt1?
Do you mean if there is same number on TransID Column in dt2, update remark column to “PREV-RVSL”?

Regards,

Yep if there are matched change remarks to PREV-RVSL and if not match just put N/A

same logic as your first solution

this might help

dt1 are basically the result just need to update remarks if there are matched = PREV-RVLS and no match = N/A with duplicate handling as the first one

Hi,

How about the following?

dt1.AsEnumerable.GroupBy(Function(r) r("Details.").ToString).SelectMany(Function(g) g.Select(Function(r1,i) dt1.Clone.LoadDataRow(r1.ItemArray.Take(dt1.ColumnCount-1).Concat({if(dt2.AsEnumerable.Where(Function(r2) r2("Trans ID").ToString=g.key).Count>i , "PREV-RVSL","N/A")}).ToArray(),false))).CopyToDataTable

Sample20220726-4v2.zip (30.8 KB)

Regards,

Okay. let me try this giving you my feedback soon tnx!

It works thank you so much!

1 Like

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