Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsEnumerable().Take

I want to take fist n number of records from a data table.

I don't want to run a loop which I already know.

I'm trying to do this

DataTable dt = dtResult.AsEnumerable().Take(n)

is the right way..?

what is the process to make this "n records" to be in another datatable..?

like image 482
Jigar Shah Avatar asked Nov 30 '25 20:11

Jigar Shah


1 Answers

Yes, this is a correct way to take first N rows from your data table. Use CopyToDataTable extension to create new data table from query result:

DataTable dt = dtResult.AsEnumerable()
                       .Take(n)
                       .CopyToDataTable();
like image 61
Sergey Berezovskiy Avatar answered Dec 02 '25 09:12

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!