How to select top n rows from a datatable/dataview in ASP.NET
How to the select top n rows from a datat开发者_StackOverflowable/dataview in ASP.NET? Currently I am using the following code, passing the table and number of rows to get the records. Is there a better way?
public DataTable SelectTopDataRow(DataTable dt, int count)
{
DataTable dtn = dt.Clone();
for (int i = 0; i < count; i++)
{
dtn.ImportRow(dt.Rows[i]);
}
return dtn;
}
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()
You could modify the query. If you are using SQL Server at the back, you can use Select top n
query for such need. The current implements fetch the whole data from database. Selecting only the required number of rows will give you a performance boost as well.
public DataTable TopDataRow(DataTable dt, int count)
{
DataTable dtn = dt.Clone();
int i = 0;
foreach (DataRow row in dt.Rows)
{
if (i < count)
{
dtn.ImportRow(row);
i++;
}
if (i > count)
break;
}
return dtn;
}
Data view is good Feature of data table . We can filter the data table as per our requirements using data view . Below Functions is After binding data table to list box data source then filter by text box control . ( this condition you can change as per your needs .Contains(txtSearch.Text.Trim()) )
Private Sub BindClients()
okcl = 0
sql = "Select * from Client Order By cname"
Dim dacli As New SqlClient.SqlDataAdapter
Dim cmd As New SqlClient.SqlCommand()
cmd.CommandText = sql
cmd.CommandType = CommandType.Text
dacli.SelectCommand = cmd
dacli.SelectCommand.Connection = Me.sqlcn
Dim dtcli As New DataTable
dacli.Fill(dtcli)
dacli.Fill(dataTableClients)
lstboxc.DataSource = dataTableClients
lstboxc.DisplayMember = "cname"
lstboxc.ValueMember = "ccode"
okcl = 1
If dtcli.Rows.Count > 0 Then
ccode = dtcli.Rows(0)("ccode")
Call ClientDispData1()
End If
End Sub
Private Sub FilterClients()
Dim query As EnumerableRowCollection(Of DataRow) = From dataTableClients In
dataTableClients.AsEnumerable() Where dataTableClients.Field(Of String)
("cname").Contains(txtSearch.Text.Trim()) Order By dataTableClients.Field(Of
String)("cname") Select dataTableClients
Dim dataView As DataView = query.AsDataView()
lstboxc.DataSource = dataView
lstboxc.DisplayMember = "cname"
lstboxc.ValueMember = "ccode"
okcl = 1
If dataTableClients.Rows.Count > 0 Then
ccode = dataTableClients.Rows(0)("ccode")
Call ClientDispData1()
End If
End Sub
If you want the number of rows to be flexible, you can add row_number
in the SQL. For SQL server:
SELECT ROW_NUMBER() OVER (ORDER BY myOrder) ROW_NUMBER, * FROM myTable
Then filter the datatable on row_number:
Dataview dv= new Dataview(dt, "ROW_NUMBER<=100", "", CurrentRows)
精彩评论