Filter datatable dynamically using DataTable.AsEnumerable()

Hi all

There is a need to filter a certain data table and the filters might change so I wanted to use filter inputs as config values.

I was able to dynamically build the expression using AsEnumerable() as below example

SampleDT.AsEnumerable().Where(function(r) r("First Name").ToString = "John") OR r("First Name").ToString = "James").CopyToDataTable

the part r("First Name").ToString = "John") or r("First Name").ToString = "James") can be more if there are more filters but the expression gets built dynamically as a string .

the problem is, I’m not able to assign this expression to a datatable type as a error at assign activity gives this

Cannot assign from type ‘System.String’ to type ‘System.DataTable’ in Assign activity

need help how I can assign this expression to a datatable type

thanks in advance

Hi @AdityaVN ,

For more clarity, Could you post a Screenshot of the Assign Activity in workflow, I do think you are trying to dynamically create a String Expression of the expression, which is not going to work.

We can manage this criteria using arrays, but we need more details on what exactly you mean by dynamic and what are the dynamic contents.

we can do following:

Assign Activity
arrFilterNames | String( ) -string Array
= {“John”,“James”}

Assign Activity:
dtFiltered =

(From d in YourDataTableVar.AsEnumerable
Let check = arrFilterNames.Any(Function (x) d("First Name").ToString.toUpper.Trim.Equals(x.ToUpper))
Where check
Select r=d).CopyToDataTable

When also empty results are to expect we can handle as described here:
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

1 Like

As an alternate also have a look on the DataTable.Select method:

where a string for the filter expression can be used. The syntax is as described here:
https://www.csharp-examples.net/dataview-rowfilter/

Hi @supermanPunch

thanks for the reply.
yes I’m trying to create a String Expression of the expression.

in the example the below is the example data table.

First Name Last Name
John L
James K
Alex P
Micheal J

I need to filter First Name, some times it can be only John details I need to filter and some times its John, James details together I need to filter and sometimes its more

I need to filter have First Name as a config value so data filtration can be dynamic.

Hi @ppr

thanks for the reply

I did try to use your suggestion but was getting this error msg at the assign activity

Strict disallows late binding

please refer to the updated statement from above. Thanks

orElse you can write it as :

DT.AsEnumerable.Where(Function(r) Arr_Str.Contains(r(0).ToString)).CopyToDataTable

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