# How to take the first set(ConfigValue) of records in a loop

Hi Team,

I have datatable called DT_ValidRecords. It has around 10k records.

Now, i have to take the first 2000 records in loop and perform some actions and again 2000 and it goes on till it reaches the end.

The highlighted screenshot , for loop will take all the records in DT_ValidRecords, now i need only 2000 and again 2000.

Regards,
Sharu

2 Likes

@sharu_priya

You can use

Dt.AsEnumerable.Take(2000).CopyToDataTable to take the first 2000…

Subsequently dt.AsEnumerable.Skip(2000).CopyToDataTable will skip the first 2000 and take the remaining

To get 2000 each time…you can create a loop with how many have been picked up and how many are remaining by using rowcount on dt

Cheers

2 Likes

Hi,

FYI, another approach:

``````chunksize = 2000
``````

then

``````arrDt = Enumerable.Range(0,(dt.Rows.Count \ chunksize) -CInt(dt.Rows.Count mod chunksize <>0)).Select(Function(i) dt.AsEnumerable.Skip(i*chunksize).Take(chunkSize).CopyToDataTable).ToArray
``````

returns datatable array which has chunksize (2000) rows (the last item has remaining rows)

Then iterate this array.

Regards,

4 Likes

Hi @Yoichi , thanks for sharing, learned new thing. Correct me if I am not wrong on below explanation

The given code creates an array of data tables by splitting a given data table (dt) into chunks of a specified size (chunkSize). It first calculates the number of chunks required by dividing the total number of rows in dt by chunkSize and rounding it up to the nearest integer, then uses Enumerable.Range to generate a sequence of integers from 0 to the number of chunks minus 1. For each integer i in the sequence, it uses LINQ to skip the first i * chunkSize rows of dt, take the next chunkSize rows, and copy them into a new data table using CopyToDataTable. The resulting data tables are then stored in an array called arrDt.

2 Likes

Hi,

I think your explanation is correct.

Regards,

1 Like

Hi Anil,

I understood it… Can you please explain how i need to use the Skip and take count

@sharu_priya

Create a loop of total items using a counter…and continue loop till the counter reaches maximum row count

Counter =0 initially

And use condition as counter < dt.rowcount

Now in the loop use `currentdt = dt.AsEnumerable.Skip(counter).Take(if(dt.Rowcount-counter<2000,dt.Rowcount-counter,2000)).CopyToDataTable`

And in the loop increment counter `counter = counter + 2000`

Now for each iteration you get 2000 items and in last iteration you get how many ever are left

2 Likes

Based on your explanation, i try to re-create the workflow.
Is this workflow correct?
Looping Data Chunks.zip (9.6 KB)

Regards

2 Likes

Can we achieve this dowhile?

@sharu_priya

Yes we can acheive using do while…condition has to be placed in do while and increment and getting table to be done inside the do while…

Cheers

1 Like

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