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

前端 未结 7 1866
轻奢々
轻奢々 2020-12-13 02:09

How to the select top n rows from a datatable/dataview in ASP.NET? Currently I am using the following code, passing the table and number of rows to get the records. Is there

7条回答
  •  北海茫月
    2020-12-13 02:48

    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;
        }
    

提交回复
热议问题