Linq Query for matching array value in data table

Hi All,

I have Data Table with Column1,Column2,Column3

I have an Array of strings

Want a solution the outcome should be an array where the array of elements should be matching with column2 values in data table.

Can someone help with the solution please…

Hi @JITU99

You can iterate through the array & inside the For Each, you can try to get the data rows like this:

For Each currentArrayItem in arrayValues

Try

 filteredDataTable = yourDt.AsEnumerable.Where(Function(row) row("COl2").ToString.Equals(currentArrayItem.ToString))
.CopyToDataTable

→ Append this data table to a master data table outside the loop.

Catch

→ Catch exception if no data row found for the currentArrayItem in the Try.

End For Each

Now, you have the desired result in the master data table, which contains all the array values data in column 2.

Hope this helps,
Best Regards.

Hi @arjunshenoy

Thanks for the quick response.
But the time complexity in this case will be more for me.
As the size of my array is more around 500-1000

I wanted to have a direct Linq Query if its possible.

Hi,

Which result do you need : string array filtered by dt OR datatable filtered by string array?

string array

arrStr = arrStr.Where(Function(s) dt.AsEnumerable.Any(Function(r) r("Column2").ToString=s)).ToArray()

datatable

dt = dt.AsEnumerable.Where(Function(r) arrStr.Contains(r("Column2").ToString)).CopyToDataTable()

Regards,

1 Like

as per my understanding you want the array which are present in the datatable

then try this

arr1.where(function(x) dt.AsEnumerable.Select(Function(row) row.Field(Of String)("ColumnName")).ToArray().contains(x)).toarray

Here arr1 is the array
dt is datatable
columnName is your column name

Result will be an array

Regards

As an alternate done with the set operation intersect

YourDataTableVar.AsEnumerable().Select(Function (x) x("Column2").ToString.Trim).Intersect(yOurStringArray).ToArray
1 Like

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