Filter datatable not using if or for each

Hi Team,

image

I have one datatable with the above structure.
I have another datatable which contains only 1 column “InvoiceNo.”.
I want to filter datatable1. Like, if datatable1 contains Values in InvoiceNo. column of the Datatable 2, i want to delete that particular row from datatable1.

Please help.

Hi @yash.choursia ,

A bit of a confusion with what is the Datatable1 and Datatable2. But eventually we should be able to perform the below steps :

  1. Get the invoices values as Array of String from the Datatable which contains only 1 column Invoices. We can do it in the below way :
invoicesList = DT1.AsEnumerable.Select(Function(x)x("Invoices").ToString).ToArray

Here, invoicesList is a variable of Type Array of String.

  1. Next, we can filter the DT2 with the invoices available in the created list like below :
DT2 = DT2.AsEnumerable.Where(Function(x)Not invoicesList.Contains(x("InvoiceNo.").ToString)).CopyToDatatable

DT2 is the datatable that is having the columns as provided in your screenshot

Direct Conversion to Datatable after Filter could result in Errors, we could handle it in the below manner :

@yash.choursia

Assign:
filteredTable = (From row1 In datatable1.AsEnumerable()
Where Not (From row2 In datatable2.AsEnumerable()
Select row2.Field(Of String)(“InvoiceNo.”))
.Contains(row1.Field(Of String)(“InvoiceNo.”))
Select row1).CopyToDataTable()

Assign:
datatable1 = filteredTable

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