Filter DataTable by LINQ function with variable parameters (In/Out)

Hello,
I would like to create a reusable function (workflow) to filter a DataTable. This function will have a few variable inputs and one output.

Inputs :

  • A source DataTable [dtSource] containing all the data
  • A destination DataTable [dtDestination] that will store the filtered data
  • An array of type string [arrFilterValues] containing the filter values (ex : {“26”, “29”, “01”})

Output :

  • The destination DataTable [dtDestination] containing the filtered data

Task :
For each value inside the [arrFilterValues] the function needs to loop on the [dtSource] in order to verify if row.Item(0).ToString.StartsWith(arrFilterValue)

Could you please suggest a LINQ expression to execute this filter?

dtDestination = dtSource.AsEnumerable().Where(Function(row) arrFilterValues.Any(Function(filter) row.Field(Of String)(0).StartsWith(filter))).CopyToDataTable()

@SONYC

2 Likes

input Arguments:

in_dtData
in_arrFilterSet
in_intColIndex

output Arguments:
out_dtResult

Code:

out_dtResult =

(From d in in_dtData.AsEnumerable()
Let val =  d(in_intColIndex).toString.Trim
Let chk = in_arrFilterSet.Any(Function (x) val.StartsWith(x))
Where chk
Select r = d).CopyToDataTable

Handling empty filter results
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

We would also recommend to safe it more on possible null values and its handling / feedback returns e.g. by Datavalidations before filtering

Both solutions work as expected.
The solution of @ppr is mandatory in case we get the chance to have an empty data table.
As for my case, I already have the resultDataTable in input argument so I need to enrich.
Thank you very much @Praveen_Mudhiraj and @ppr .

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