Copy specific number of rows from one datatable to another

Hi,

I have identified row index of 5 and 50. How do I copy all the rows between row 5 and row 50 to another datatable?

thanks.

Hi @sophiey ,

Maybe we could use Skip and Take methods :

NewDT = DT.AsEnumerable.Skip(5).Take(50-5).CopyToDatatable

Here, DT is the Input Datatable

Used Constant values, but could be replaced with the variables accordingly if used.

Let us know if it doesn’t work.

@sophiey

hi,try this

dt1.AsEnumerable.skip(4).Take(49-5).CopyToDataTable

Use a For Each Row activity with the Original DataTable.
Inside the loop, use an If activity with the condition: “rowIndex >= 5 and rowIndex <= 50”
Inside the If activity, add an Add Data Row activity to add the current row to the New DataTable

Hi @sophiey

Try this

’ Define the start and end row indices
startRowIndex= 5 ’ Change this to the actual start row index
endRowIndex= 50 ’ Change this to the actual end row index

’ Use LINQ to filter and copy the rows
newDataTable= originalDataTable.AsEnumerable() .Where(Function(row, rowIndex) rowIndex >= (startRowIndex - 1) AndAlso rowIndex <= (endRowIndex - 1)).CopyToDataTable()

Hope this helps!!

Hi @sophiey

// Assuming you have a source DataTable called sourceDataTable
// and a destination DataTable called destinationDataTable

DataTable resultDataTable = sourceDataTable.AsEnumerable()
    .Where((row, index) => index >= 5 && index <= 50)
    .CopyToDataTable();

// Now, resultDataTable contains the filtered rows, which you can assign to your destination DataTable.

Hope it helps!!

thanks all for the replies.

I’m looking to optimise my codes. If i want to find the row index of the currentRow(0) = “xxx” how do i go about it? there is no headers in the dt.

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