How to use Linq Query stored in a String variable inside Assign Activity

I have a scenario where im changing an existing linq query and then its stored in a string variable , i want to use it in assign activity

Dt (Datatable) = LinqQueryVar

but as u guess the string variable isnt representing the code format that is needed

You can’t. UiPath doesn’t support executing code stored in a variable.

i knew , but isnt there any kind of ‘Var’ Datatype that can be used ?

Also my question regarding this is , how sql activties use input as string but actually converting into sql code format in the background

No, as I said UiPath doesn’t support executing code that is stored in a variable.

Because SQL is a different thing, and supports it.

Hi @indiedev91

If you have a LINQ query stored as a string variable and you want to execute it on a DataTable, you can achieve this by dynamically creating and running the LINQ query using the DataTable.Select method.

Thanks!!

1 Like

can you give me an example ?

Here is a sample Linq Query stored in String Variable

( From r In in_InputFile
Group By z=r("Country").ToString.Trim Into grp = Group
Let temp0 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("ABC")).Count
Let temp1 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("POR")).Count
Let temp2 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("PQR")).Count
Let temp3 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("DEF")).Count
Let temp4 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("wxy")).Count
Let temp5 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("Avr")).Count
Let temp6 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("ABc")).Count
Let temp7 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("MNP")).Count
Let temp8 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("GHI")).Count
Let temp9 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("xyz")).Count
Let temp10 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("FHI")).Count
Let temp11 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("STV")).Count
Let newRows = New Object() { grp(0)("Country").ToString,temp0.ToString,temp1.ToString,temp2.ToString,temp3.ToString,temp4.ToString,temp5.ToString,temp6.ToString,temp7.ToString,temp8.ToString,temp9.ToString,temp10.ToString,temp11.ToString, temp0+temp1+temp2+temp3+temp4+temp5+temp6+temp7+temp8+temp9+temp10+temp11}
Select dt_Output.Rows.Add(newRows)).CopyToDataTable

DataTable.Select does not exactly support LINQ queries, but another syntax.

You can check here for some examples. C# DataTable Select Example - Dot Net Perls - https://www.dotnetperls.com/

1 Like

Hello @indiedev91

Assign:
FilteredDt = Dt.AsEnumerable().Where(Function(row) YourFilterConditionBasedOnLinqQueryString).CopyToDataTable()

Thanks & Cheers!!!

can you give an example on how can i use this linq query in the string and which datatable do i mentioned before AsEnumerable

( From r In in_InputFile
Group By z=r("Country").ToString.Trim Into grp = Group
Let temp0 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("ABC")).Count
Let temp1 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("POR")).Count
Let temp2 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("PQR")).Count
Let temp3 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("DEF")).Count
Let temp4 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("wxy")).Count
Let temp5 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("Avr")).Count
Let temp6 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("ABc")).Count
Let temp7 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("MNP")).Count
Let temp8 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("GHI")).Count
Let temp9 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("xyz")).Count
Let temp10 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("FHI")).Count
Let temp11 = grp.where( Function(x) x("Designation").ToString.Trim.Equals("STV")).Count
Let newRows = New Object() { grp(0)("Country").ToString,temp0.ToString,temp1.ToString,temp2.ToString,temp3.ToString,temp4.ToString,temp5.ToString,temp6.ToString,temp7.ToString,temp8.ToString,temp9.ToString,temp10.ToString,temp11.ToString, temp0+temp1+temp2+temp3+temp4+temp5+temp6+temp7+temp8+temp9+temp10+temp11}
Select dt_Output.Rows.Add(newRows)).CopyToDataTable

@indiedev91

Dim linqQueryString As String =
“From r In in_InputFile " &
“Group By z=r(”“Country””).ToString.Trim Into grp = Group " &
“Let temp0 = grp.Where(Function(x) x(”“Designation”“).ToString.Trim.Equals(”“ABC”“)).Count " &
“Let temp1 = grp.Where(Function(x) x(”“Designation””).ToString.Trim.Equals(““POR””)).Count " &
“Let temp2 = grp.Where(Function(x) x(”“Designation”“).ToString.Trim.Equals(”“PQR”“)).Count " &
“Let temp3 = grp.Where(Function(x) x(”“Designation””).ToString.Trim.Equals(““DEF””)).Count " &
“Let temp4 = grp.Where(Function(x) x(”“Designation”“).ToString.Trim.Equals(”“wxy”“)).Count " &
“Let temp5 = grp.Where(Function(x) x(”“Designation””).ToString.Trim.Equals(““Avr””)).Count " &
“Let temp6 = grp.Where(Function(x) x(”“Designation”“).ToString.Trim.Equals(”“ABc”“)).Count " &
“Let temp7 = grp.Where(Function(x) x(”“Designation””).ToString.Trim.Equals(““MNP””)).Count " &
“Let temp8 = grp.Where(Function(x) x(”“Designation”“).ToString.Trim.Equals(”“GHI”“)).Count " &
“Let temp9 = grp.Where(Function(x) x(”“Designation””).ToString.Trim.Equals(““xyz””)).Count " &
“Let temp10 = grp.Where(Function(x) x(”“Designation”“).ToString.Trim.Equals(”“FHI”“)).Count " &
“Let temp11 = grp.Where(Function(x) x(”“Designation””).ToString.Trim.Equals(““STV””)).Count " &
“Let newRows = New Object() { grp(0)(”“Country”").ToString,temp0.ToString,temp1.ToString,temp2.ToString,temp3.ToString,temp4.ToString,temp5.ToString,temp6.ToString,temp7.ToString,temp8.ToString,temp9.ToString,temp10.ToString,temp11.ToString, temp0+temp1+temp2+temp3+temp4+temp5+temp6+temp7+temp8+temp9+temp10+temp11} " &
“Select dt_Output.Rows.Add(newRows)).CopyToDataTable”

Dim resultDataTable As DataTable = dt_InputFile.AsEnumerable().GroupBy(Function(row) row(“Country”).ToString.Trim) _
.Select(Function(grp) New With {
.Country = grp.Key,
.Temp0 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“ABC”)),
.Temp1 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“POR”)),
.Temp2 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“PQR”)),
.Temp3 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“DEF”)),
.Temp4 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“wxy”)),
.Temp5 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“Avr”)),
.Temp6 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“ABc”)),
.Temp7 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“MNP”)),
.Temp8 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“GHI”)),
.Temp9 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“xyz”)),
.Temp10 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“FHI”)),
.Temp11 = grp.Count(Function(x) x(“Designation”).ToString.Trim.Equals(“STV”)),
.Total = grp.Count()
}).Select(Function(anon) New Object() {anon.Country, anon.Temp0, anon.Temp1, anon.Temp2, anon.Temp3, anon.Temp4, anon.Temp5, anon.Temp6, anon.Temp7, anon.Temp8, anon.Temp9, anon.Temp10, anon.Temp11, anon.Total}).CopyToDataTable()

The different answers from above should have cleared the question:

When referring to your case:

We also answered in your other topic that we can have dynamics when using an intermediate Datastructure as a first step and generate from this the final target output

This can be done with Linq/Essential Activities and should not lead to a long modelling sequence

1 Like

Cross reference (Solution)