Linq query for matching and joining the data table

Hi All,

Am very bad in Linq query so need your help. I am matching 2 coloumns data from diff data table and joining both the datatables basis match. Please suggest me linq qeury. I tried JOIN Data tables, but it didnt work as its showing 0 match but when am checking manually, there is match.

Thanks for your help.

Hi @taruna.mehra

Can you please provide more details about the structure of your data tables and the specific columns you want to match? Also, let us know if you have any specific conditions or criteria for the matching process.

Best Regards.

Hi @taruna.mehra

You can try this, Dt1.AsEnumerable.Except(Dt2.AsEnumerable,DataRowComparer.Default).Count>0

I hope It may helps you!!

Hi,

Need to match J column from dt1 and K column from dt2 then need to join dt & dt2.

Thanks
Taruna

hI,

Need to match columns data dt1 and dt2. This won’t work i guess.

Thanks
Taruna

Hi @taruna.mehra

Try this,

(From a In Dt1.AsEnumerable()
            Join b In Dt2.AsEnumerable()
            On a("matching_Column") Equals b("Matching_Column")
            Select OutputDataTable.Rows.Add({a("Matching_Column"), a("Col_1"), a("Col_2"), b("Col_3"), b("Col_4")})
).CopyToDataTable()

Regards,

@taruna.mehra

Can you try this query:

Dim joinedData = From row1 In dt1.AsEnumerable()
                 Join row2 In dt2.AsEnumerable()
                 On row1.Field(Of String)("J") Equals row2.Field(Of String)("K")
                 Select New With
                 {
                     .Column1 = row1.Field(Of String)("J"),
                     .Column2 = row2.Field(Of String)("K")
                 }

Dim dtResult As New DataTable()
dtResult.Columns.Add("Column1")
dtResult.Columns.Add("Column2")

For Each item In joinedData
    Dim newRow As DataRow = dtResult.NewRow()
    newRow("Column1") = item.Column1
    newRow("Column2") = item.Column2
    dtResult.Rows.Add(newRow)
Next

Hope this helps,
Best Regards.

Invoke code?

@taruna.mehra

Yes, you can try it in Invoke Code activity.

Best Regards.

Hi, its showing error.

Hi @taruna.mehra

Cna you try this one -

var query = from row1 in dt1.AsEnumerable()
join row2 in dt2.AsEnumerable()
on row1.Field(“Column1”) equals row2.Field(“Column2”)
select new
{
Column1 = row1.Field(“Column1”),
Column2 = row2.Field(“Column2”),
// Add other columns you want to include in the result
};

DataTable resultTable = query.CopyToDataTable();

I hope it helps!

Thanks!!

1 Like