Hi,
I have a Sample datatable like below,
Dept TotalHead
Engineering 500
Biotech. 450
Mechanical 230
Electric 100
I need to convert the Column Totalhead from String to Int datatype without Looping since I need to use filter data table for filtering out Total Head < 200
You can achieve this in UiPath without using a loop by using the “Filter DataTable” activity along with the “Assign” activity. Here’s a step-by-step guide:
-
Read your sample data table into a DataTable variable, let’s call it
dtSample
. -
Use the “Assign” activity to create a new DataTable variable, let’s call it
dtFiltered
. -
Use the “Assign” activity to create an expression to filter out rows where TotalHead is less than 200. The expression would look something like this:
dtFiltered = dtSample.Select(“[TotalHead] < 200”).CopyToDataTable()
This will create a new DataTable containing only the rows where TotalHead is less than 200.
-
Now, you need to convert the “TotalHead” column from String to Int datatype. You can do this by using the “For Each Row” activity.
-
Inside the “For Each Row” activity, use an “Assign” activity to convert the “TotalHead” column value from String to Int. You can use the
Convert.ToInt32()
method for this conversion. Here’s an example:row(“TotalHead”) = Convert.ToInt32(row(“TotalHead”))
This will convert the “TotalHead” column value from String to Int for each row in the DataTable.
-
Now,
dtFiltered
DataTable will have the “TotalHead” column converted to Int datatype and filtered for values less than 200. -
You can use this
dtFiltered
DataTable for further processing or any other operations you need.
Thank you,
Akhil
=> Read Range Workbook
Output-> dt
=> Use below code in Invoke Code:
dt.AsEnumerable().ToList().ForEach(Sub(row) row("TotalHead") = If(IsNumeric(row("TotalHead").ToString()), Convert.ToInt32(row("TotalHead")), 0))
Invoked Arguments:
=> Use Filtered Data Table activity and configure like below:
Output-> filteredDt
=> Write Range Workbook filteredDt
to new sheet.
Sequence7.xaml (8.7 KB)
Regards
we can put the conversion within a LINQ which is also doing the filtering
Assign Activity
dtFiltered =
(From d in dtData.AsEnumerable
let thi = CInt(d("Total Head").toString.Trim)
Where thi < 200
Select r=d).CopyToDataTable
Handling empty results:
[FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum
About LINQ
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum
Possible improvements:
- handling non parseable values