Search code examples
c#linqdatatablelinq-to-dataset

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..?


Solution

  • 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();