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.
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
FYI, another approach:
chunksize = 2000
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.
@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.
I think your explanation is correct.
I understood it… Can you please explain how i need to use the Skip and take count
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
@Yoichi , @ABHIMANYU_THITE1,
Based on your explanation, i try to re-create the workflow.
Is this workflow correct?
Looping Data Chunks.zip (9.6 KB)
Can we achieve this dowhile?
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…
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.