How To Get Top N Records from a DataTable – In UiPath

Sometimes we might be asked to process only the top N records out of the total available records in the DataTable.

Let us see how to implement it!!!

 new_DataTable=old_DataTable.AsEnumerable().Take(N).CopyToDataTable()

Note:
Replace old_DataTable with your DataTable
N = 1,2,3….

Example

Implementation using UiPath :

Let us implement a workflow which takes a sample DataTable and displays the top N records of it, according to our supplied input.

Step 1:
Drag “Build Data Table” activity into the designer panel and insert some sample data into it.

UiPath Get Top N records from datatable 1UiPath Get Top N records from datatable 2

Step 2:
Drag “Assign” activity into the designer panel and populate it with the below code.

dt_Sample.AsEnumerable().Take(3).CopyToDataTable()

UiPath Get Top N records from datatable 3.png

Step 3:
Drag “Output Data Table” activity into the designer panel to convert Datatable to String type.

UiPath Get Top N records from datatable 4UiPath Get Top N records from datatable 5

Step 4:
Drag “Message Box” activity into the designer panel to display the modified data table.

UiPath Get Top N records from datatable 6.PNG

Step 5:
Finally, let’s run the project.

UiPath Get Top N records from datatable 7.PNG

Click here to download the Source Code…

Hope it has helped you…

Leave a Reply

%d bloggers like this: