Merging Datatable based on condition

This is input sheet
image
image

i need to merge and update amount column
image

linq code

@sathya_giri

(From row1 In dt1.AsEnumerable()
 Select row1
).Union(
From row2 In dt2.AsEnumerable()
Where Not dt1.AsEnumerable().Select(Function(r) r("ID").ToString()).Contains(row2("ID").ToString())
 Select row2
).CopyToDataTable()

its not updating as per requirement brother
\

@sathya_giri

Sorry i dont see your requirement properly i will change

you can see the image of output

@sathya_giri
Assign activity:
mergedDataTable = (From row1 In firstInput.AsEnumerable()
Join row2 In secondInput.AsEnumerable()
On row1.Field(Of Integer)(“ID”) Equals row2.Field(Of Integer)(“ID”)
Select firstInput.Clone().LoadDataRow({row1(“ID”), row1(“amount”), row2(“amount”)}, False)).CopyToDataTable()

code is error brother…

@sathya_giri

Check this its working for me:

(From row1 In dt1.AsEnumerable()
                   Group Join row2 In dt2.AsEnumerable()
                   On row1("ID") Equals row2("ID") Into Group
                   From row2 In Group.DefaultIfEmpty()
                   Select dt1.Clone().LoadDataRow({row1("ID"), If(row2 IsNot Nothing, Math.Max(CInt(row1("Amount")), CInt(row2("Amount"))), CInt(row1("Amount")))}, False)).CopyToDataTable()

@sathya_giri

mergedDataTable = (
    From row1 In dt1.AsEnumerable()
    Group Join row2 In dt2.AsEnumerable()
    On row1.Field(Of Integer)("ID") Equals row2.Field(Of Integer)("ID") Into grp = Group
    From row2 In grp.DefaultIfEmpty()
    Select If(row2 Is Nothing, row1, If(row1.Field(Of Decimal)("Amount") > row2.Field(Of Decimal)("Amount"), row1, row2))
).CopyToDataTable()

Please try this now it is working fine as per your requirement

thankyou @supriya117 @rlgandu @Krishna_Raj

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