Deleting middle rows in dataTable

Hey,

I have a question - I have a DataTable with three columns and 23 rows (including header). I want to have only the first five and last five rows. I am not sure how can I do it?

I do not want to delete rows between the first five and the last five because that DataTable may change in the future (some web scraping makes it so number of rows may increase). I just want to extract the first five and last five rows, maybe into a new DataTable. Can you guys help me?

This is the screenshot of exported DataTable to excel:

1 Like

Give a try on following:

Assign Activity
LHS: dtReduced | DataType: DataTable
RHS:
dt.AsEnumerable.Where(Function (x,i) i < 5 or i > dt.Rows.Count - 6).CopyToDataTable

dt = your Origin DataTableVar

2 Likes

Hey @senek

Here you go…

nmnithinkrishna_TakeFirst5Last5RowsFromDT.zip (78.3 KB)

Thanks
#nK

1 Like

Thank you!! Can you tell me (or maybe explain it to me like I’m dumb) why is “.AsEnumerable.Where” used? And do you know where I can find more info about such functions? I have never heard about them. Cheers and much thanks. You made my project easier a ton.

it about LINQ

we used an indexed where statement and i is representing the poisition/row index from the processed collection of datarows

1 Like

Amazing. Thank you very much.

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