Copy first N rows from Datatable from specific column and get next set

Hi All,

I would like to have a loop that copy first 20 rows from datatable and save to variable and copy next 20 rows from same datatable and save to variable.

Data:

Column1 Column2
12 wqer
12 wewr
112
212

Hi @Birdi

Use dtDt.Asenumerable.Take(20). Copy Todatatable()
Based on that try to loop it

Thanks
Ashwin.S

3 Likes

How Do I select the specific column to copy? CopyToDatatable copies whole datatable

Hi @Birdi
Check this NewDt=Dt.Select("[Column Name]<>’’"").CopytoDatatable()
Thanks
Ashwin S

@AshwinS2 thanks for help. I think i might need to clarify my question.

I would like to copy first 20 rows from datatable and save to variable and put in SAP then copy next 20 rows from datatable and save to variable and put in SAP.

Does that help?

The first answer provided does exactly what you ask. It takes the first 20 rows of the datatable, and creates a new datatable containing only those 20 rows. If you wish to do this for the entire datatable 20 rows at a time, then you should assign an integer variable as a counter, and also include a .Skip() function. So it would be something like this:

Create a variable (of type integer) - i'll call this RowCounter

While RowCounter <= OriginalDT.Rows.Count
Assign YourNewDataTableWith20Rows (of type datatable) = OriginalDT.AsEnumerable.Skip(RowCounter).Take(20).CopyToDatatable()
(do processing here, such as pasting into SAP)
Assign RowCounter = RowCounter + 20
2 Likes

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