How to select top n rows from a datatable/dataview in ASP.NET How to select top n rows from a datatable/dataview in ASP.NET asp.net asp.net

How to select top n rows from a datatable/dataview in ASP.NET


In framework 3.5, dt.Rows.Cast<System.Data.DataRow>().Take(n)

Otherwise the way you mentioned


I just used Midhat's answer but appended CopyToDataTable() on the end.

The code below is an extension to the answer that I used to quickly enable some paging.

int pageNum = 1;int pageSize = 25;DataTable dtPage = dt.Rows.Cast<System.Data.DataRow>().Skip((pageNum - 1) * pageSize).Take(pageSize).CopyToDataTable();


myDataTable.AsEnumerable().Take(5).CopyToDataTable()