Best Method to Split DataTable Rows into Fixed-Size Chunks (Without Using For Each Loops)

Hi Team,

I have a large DataTable, and I want to split it into multiple smaller DataTables—each containing a fixed number of rows (for example, 500 rows per chunk).

Is there a faster or more efficient method to achieve this in UiPath without relying on standard For Each loops, especially when working with very large datasets?

Example

Input DataTable: 10,000 rows
Desired Output:

  • DataTable_1 → Rows 1–500
  • DataTable_2 → Rows 501–1000
  • DataTable_3 → Rows 1001–1500
  • …and so on

I’m looking for an optimized or built-in approach (LINQ, .NET methods, Invoke Code, or any other best practice) that can perform this “row chunking” efficiently.

Any suggestions or sample code would be really helpful.

Thanks!

@ba19c1e84c03a81430ebd2ea1

you can use Datatablevariable.AsEnumerable.Chunk(500) this given as input for loop..will send 500 rows each time as a set and isnide loop for each iteration you would get 500 or less items ..for last iteration if 500 are not there then it would send how many ever are left

cheers

1 Like

Hi @ba19c1e84c03a81430ebd2ea1

You can try through LINQ, change code accordingly:
chunks = dt.AsEnumerable().
Select(Function(r,i) New With {.Row=r, .Index=i}).
GroupBy(Function(x) x.Index \ 500).
Select(Function(g) g.Select(Function(x) x.Row).CopyToDataTable()).
ToList()

Happy Automation

Welcome to UiPath Community @ba19c1e84c03a81430ebd2ea1 ,

LINQ allows you to chunk a DataTable in a vectorized manner without iterating each row inside UiPath activities.

Example – Chunk size = 500 rows

Enumerable.Range(0, CInt(Math.Ceiling(dt_ChunkInput.Rows.Count / ChunkSize))) _
.Select(Function(i) dt_ChunkInput.AsEnumerable() _
.Skip(i * ChunkSize) _
.Take(ChunkSize) _
.CopyToDataTable()) _
.ToList()
ChunkDataTableToList.zip (119.1 KB)

Produces: List(Of DataTable)
Each DataTable contains exactly 500 rows (last one may contain fewer)
Much faster than traditional For Each Row

2 Likes

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