Hi all, hope you can help me solve this:
I have 2 data tables from 2 different sheets.
I need to go through the data tables and focus on 1 column from each table: “Customers” from dt1 and “Customer names” from dt2. I need to extract the additional names that are in “Costumers” and not in “Customer names”.
Please note:
There will[probably] duplicate names on both columns that should be removed.
Not sure if the names will be parallel to each other…
Dt1 and dt2 are the datatables containing each table data
requireddt = Dt1.AsEnumerable.Where(function(x) Not dt2.AsEnumerable.Any(function(y) y("CustomerNames").ToString.Equals(x("Customer").ToString)).Distinct(function(x) x("Customer").ToString).CopyToDataTable
If you think there might not be 1 rows also which is not matched then to avoid exception first use if condition and instead of .CopyToDatatable use .Count>0 and then on the then side use .CopyToDataTable and on else side you have no unmatched rows at all
Here is dummy data. Expected output is Dan and Kathy. [after removing duplicates, these 2 names don’t appear in the first column of the 2nd dt] DummyData.xlsx (9.6 KB)
' Assuming dt1 and dt2 are your DataTables
Dim distinctCustomers_dt1 = dt1.AsEnumerable().Select(Function(row) row("MEMBER NAME").ToString()).Distinct().ToList()
Dim distinctCustomerNames_dt2 = dt2.AsEnumerable().Select(Function(row) row("Patient").ToString()).Distinct().ToList()
' Find additional names
Dim additionalNames = distinctCustomers_dt1.Except(distinctCustomerNames_dt2).ToList()
' Create a new DataTable to store the additional names
resultDataTable = New DataTable("AdditionalNames")
resultDataTable.Columns.Add("AdditionalName", GetType(String))
' Add additional names to the DataTable
For Each name In additionalNames
resultDataTable.Rows.Add(name)
Next