COMPARE TWO DATATABLES AND GET NEW DATATABLES WITHOUT USING LOOPS!

I could be wrong, but since you need to update a column with a new value, you will need to use atleast 1 loop for each table you need to update. If you didn’t need to update a value, then LINQ alone would work.

So, I think you can use a combination of LINQ and ForEach. - Note that ForEach loops have been improved to be much more efficient and use less resources. (like 10k rows only takes 1-2secs to loop through)

Here is an example:

If Not demo1.Columns.Contains("Status")
     Add Data Column "Status
If Not demo2.Columns.Contains("Status")
     Add Data Column "Status

ForEach row In demo1
    Assign matchedRows = demo2.AsEnumerable.Where(Function(r) r("Name").ToString.Trim.Equals(row("Name").ToString.Trim) And r("Age").ToString.Trim.Equals(row("Age").ToString.Trim) And r("Place").ToString.Trim.Equals(row("Place").ToString.Trim) ).ToArray
    Switch (Boolean): condition: matchedRows.ToArray.Count > 0
        Case True:
            Assign row("Status") = "Repeated"
            ForEach rowMatched In matchedRows //TypeArgument DataRow
                Assign rowMatched("Status") = "Repeated"
        Case False:
            Assign row("Status") = "New"

ForEach row In demo2.AsEnumerable.Where(Function(r) r("Status").ToString.Trim = "" ).ToArray //TypeArgument DataRow
    Assign row("Status") = "Mitigated"

You can also use something like String.Join(","r.ItemArray) = String.Join(",",row.ItemArray) when comparing each column to check all columns more dynamically if desired.

I hope this helps get you in the right direction.

Like I said, if you need to change values in a datatable, I don’t think you can get away with not using a For each loop.

Regards.

3 Likes