Need help with Nested for each

Hi all ,
I want to check DT1 column contains value of column from another DataTable and if it returns Yes ,then I need to update one value , can you please help me to do this in C# Linq query .

For Example
DT1
image
DT2
image

output DT should be
image

if DT1[“Name”] contains DT[“Name”]
then I should Assign DT1["Address]=DT2[“Address”]
I could do that with foreach loop , but its taking more time because of huge data .
Thank you.

@Manchu

try this :

DT_1.AsEnumerable.Join(DT_2.AsEnumerable, row1 => row1["Name"],row2 => row2["Name"],(row1, row2) => { row1["Address"] = row2["Address"]; return row1; }).ToList;

cheers!!!

Hello @Manchu

  1. Assign activity:
    dt1.AsEnumerable().Join(dt2.AsEnumerable(),
    Function(row1) row1(“Name”).ToString(),
    Function(row2) row2(“Name”).ToString(),
    Function(row1, row2) Tuple.Create(row1, row2))
    .Where(Function(pair) Not String.IsNullOrWhiteSpace(pair.Item1(“Name”).ToString()) AndAlso Not String.IsNullOrWhiteSpace(pair.Item2(“Name”).ToString()))
    .ToList()

  2. For Each activity (TypeArgument: Tuple(Of DataRow, DataRow)):
    a. Assign activity:
    pair.Item1(“Address”) = pair.Item2(“Address”)

Thanks & Cheers!!!

Hi

var updatedDT1 = DT1.AsEnumerable()
.Join(DT2.AsEnumerable(),
dt1Row => dt1Row[“Name”],
dt2Row => dt2Row[“Name”],
(dt1Row, dt2Row) => {
dt1Row[“Address”] = dt2Row[“Address”];
return dt1Row;
})
.CopyToDataTable();

Try this linq query

@Manchu

Try this Linq:

DT_1.AsEnumerable.Join(DT_2.AsEnumerable, row1 => row1[“Name”],row2 => row2[“Name”],(row1, row2) => { row1[“Address”] = row2[“Address”]; return row1; }).ToList;

Hi @Manchu
Use Invoke VBA Code activity and use the below code
DT1.AsEnumerable().ToList().ForEach(
Sub(row1)
Dim nameToSearch As String = row1(“Name”).ToString()
Dim matchingRow = DT2.AsEnumerable().FirstOrDefault(Function(row) row(“Name”).ToString() = nameToSearch)
If matchingRow IsNot Nothing Then
row1(“Address”) = matchingRow(“Address”)
End If
End Sub)

Note:
nameToSearch= Variable is used to search for a matching row in DT2
** Take In argument for nameToSearch varibale (Value to match in DT2)
** Set the Argument for both DT1 & DT2 with In/Out Argument.

Hope this will help you.

Cheers!

Hey Yogeesh ,
I tried it on my code it is not working .will it check dt1Row[“Name”] Contains (dt2Row[“Name”]),but it checks dt1Row[“Name”] ==dt2Row[“Name”]

What if two different people are both named Alison but with different last names?

Hi ,
It dosen’t matter , this is just an example .

Hi @Dilli_Reddy , I want to check dt1Row[“Address”].Contains(dt2Row[“Address”]); , How can I write this in Linq.