Extract two rows from data table

As i have a data table consist of 5 rows, while converting that table into JSON I need first two rows only.
How we can get only two rows from the data table.

YourDataTableVar.AsEnumerable.Take(2).CopyToDataTable

will take the first 2 rows form the dt and return a datatable

1 Like

@agathiyanv
It will return 2 rows
Dt2 = Dt1.Clone Dt2=Dt1.AsEnumerable().Take(2).CopyToDataTable()

1 Like

Hi @agathiyanv ,

Additionally or rather directly, we could maybe convert to Json using SerializeObject Method like below :

Newtonsoft.Json.JsonConvert.SerializeObject(DT.AsEnumerable.Take(4).CopyToDataTable)
1 Like

Can we specify column also , for eg I have 4 rows and each row having 5 column but I need to only 2 row and 4 column.
Can we do like this?

Yes you can my idea of doing this is

Before using this you can use filter datatable and filter only the column you want and filter them with the datatable dt and store them in same dt and use that updated filtered dt in your expression to take 2 rows
image

Hope this Helps

Regards
Sudharsan

1 Like

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