How to match row value using linq and update status column as matched or not matched

i have two datatable, i have to compare row of column1 of dt1 with row column 1 of dt 2

if value of both cell matched i have to update dt1 column Status as matched or not matched

Please help me how to do with Linq

@aslam_ali1

dt1 = (From row1 In dt1.AsEnumerable()
Join row2 In dt2.AsEnumerable()
On row1(“Column1”).ToString() Equals row2(“Column1”).ToString()
Select dt1.Clone().LoadDataRow({row1(“Column1”), If(row2 Is Nothing, “Not Matched”, “Matched”)}, False)).CopyToDataTable()

@aslam_ali1

try with invoke code

dt1.Columns.Add(“Status”)
For Each r1 As DataRow In dt1.AsEnumerable
For Each r2 As datarow In dt2.AsEnumerable
If r1(0).ToString.trim.Equals(r2(0).ToString.Trim) Then
r1(“Status”)=“Matched”
Else
r1(“status”)=“Not Matched”
End If
Next
Next

cheers

Hi @aslam_ali1 ,

Can you please try below code,

DT.AsEnumerable().ToList().ForEach(Sub(row) row(“Column3”) = If(row(“Column1”).ToString = row(“Column2”).ToString, “Matched”, “Not Matched”))

Thanks,
Sandhiya P

Hi @aslam_ali1

You can try this using Invoke Code activity

Code:

For Each row1 As DataRow In dt1.Rows
    Dim valueToMatch As String = row1("Column1").ToString()
    Dim matchingRow As DataRow = dt2.AsEnumerable().FirstOrDefault(Function(row) row("Column1").ToString() = valueToMatch)

    If matchingRow IsNot Nothing Then
        row1("Status") = "Matched"
    Else
        row1("Status") = "Not Matched"
    End If
Next

Hope this helps!!


@rlgandu getting this error

can u please share the xaml file. Im not understanding
im beginner to Linq

@sandhiya.ppp

I have two different data table

if possible please share the xaml also

thanks

@aslam_ali1

BlankProcess12.zip (42.6 KB)

@aslam_ali1

dt1.AsEnumerable.ToList.ForEach(Sub(row) row("Status") = If(dt2.AsEnumerable.Any(Function(dr) dr("Column1").ToString = row("Column1").ToString), "matched", "not matched"))

Try this its helps you :

cheers…!

@aslam_ali1

try this linq query

use add data column to add a column name it as status

dt=(From row In dt1.AsEnumerable()
Let a = If(dt2.AsEnumerable.Any(Function(r) r(0).ToString.trim.Equals(row(0).ToString.trim)),“Matched”,“Not Matched”)
Select dt1.Clone.LoadDataRow({row(0).ToString,row(1).ToString,a},False)).CopyToDataTable

cheers

Thank you @lrtetala it is working

1 Like

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