Assign variables in Select

Hi @andrzej.kniola,

Just in case my previous reply confuse you.

With that syntax, I get the error: The expression contains undefined function call DT.Columns()

May I have your advice again ? Thanks so much.

DT.Select(β€œ[" & DT.Columns(0).ColumnName & "] = '” & stringVariable &"’").CopyToDatatable
or in a more readable way:

string filterTemplate = "[{0}] = '{1}'"
string filter = String.Format(filterTemplate, DT.Columns(0).ColumnName, stringVariable)
DT = DT.Select(filter).CopyToDatatable

2nd option makes it also much easier to understand and/or modify as needed (and is very similar to what was in the Academy if I remember correctly).

1 Like

Thanks, let me try.

I have just passed only the basic one…:sweat_smile:

Just a quick response to your option 1:

I typed: DT1.Select(β€œ[”& DT1.Columns(1).ColumnName & β€œ] = '” & StringA &β€œ'”).CopyToDatatable

but get an error: Assign : Cannot perform β€˜=’ operation on System.Double and System.String.

For Option 2, would you mind telling me what does β€œ[{0}] = β€˜{1}’” mean in the filterTemplate ?

Hi @andrzej.kniola,

My bad, I think your option 1 should work, it is the problem of my excel file. I think I just need to change the values to string in my excel.

Hi @andrzej.kniola ,

Just would like to share, after your advice plus some trials, that the datatable takes column0 as the column name for column index = 0, and column1 as the column index = 1, etc. The following works for me also:
DT1.Select(β€œ[Column0] = '” & StringA &β€œ'”).CopyToDatatable

@aksh1yadav I want to use contains in select instead of equal can you please help me out of this??

@Bachali you can use Filter Data Table activity

https://activities.uipath.com/docs/filter-data-table

@indra I think it will work only in excel application scope but I’m using data scrapping and taking that in datatable. for that datatable I want to check whether my requirement is contains r not by using select function

@Bachali you can use like keyword

How to use contains in a select from datatable

cdr.Select(β€œClient=”+β€œβ€˜β€œ+serverName+”’”).CopyToDataTable

@aksh1yadav Hi i have tried your way, apparently mine gives me error. Could you help me with my lines