极限数数据表中的行数 [英] limit no. of rows in a datatable
问题描述
代码
dt.AsEnumerable().Take( 15 ).CopyToDataTable(dtnew);
但这会引发错误,如No overload for method ''CopyToDataTable'' takes ''1'' arguments
那么,如何限制数据表dt?
请帮忙!
您可以尝试使用下面的LInQ代码示例:
DataView dataView = 新 DataView(dataTable); dataView.RowFilter = 字符串 .Format(" ,DateTime.Now); dataView.Sort = " ; dataTable = dataView.ToTable(); 同时(dataTable.Rows.Count > _rowLimit) { dataTable = dataTable.AsEnumerable().Skip( 0 ).Take( 50 ).CopyToDataTable(); } 返回 dataTable;
如果您只需要数据库中的前15行,最好使用它sql命令
选择 TOP ( 15 )colname 来自表名;
这将返回前15行,并使用数据表填充此结果....
语句
dt.AsEnumerable().Take( 15 )
问题中给定的值检索前15条记录,并返回DataRow
的IEnumerable
,如此处所述
http://msdn.microsoft.com/en-us/library/bb503062.aspx [ ^ ]
问题不是因为Take(15)
,而是因为CopyToDataTable
方法.
CopyToDataTable
具有three over loads
,如此处所述
http://msdn.microsoft.com/en-us/library/bb360272 [ ^ ]
第一个重载不带任何参数,并返回DataTable
.
而Second
和Third
重载将DataTable
作为参数,并将查询返回的行复制到此DataTable
中.这样做时,两个重载中还必须同时包含LoadOption
参数.
因此,仅需1个参数的CopyToDataTable
方法就不会过载.因此,将引发错误.
LoadOption
参数提供了此处说明的选项
http://msdn.microsoft.com/en-us/library/system.data.loadoption [^ ]
因此,要执行查询并提供前15条记录,可以将其称为
var dtnew = dt.AsEnumerable().Take( 15 ). CopyToDataTable();
或作为
dt.AsEnumerable().Take( 15 ).CopyToDataTable(dtnew,LoadOption.OverwriteChanges);
hi! i have got a datatable dt which is retrieving all the records but i need only the top 15 so how can i restrict the no. of records in datatable. Please help i tried few codes like
dt.AsEnumerable().Take(15).CopyToDataTable(dtnew);
but this throws an error as No overload for method ''CopyToDataTable'' takes ''1'' arguments
So, how can i restrict the datatable dt?
Please help!
Thanking You in advance!
You can try using the below LInQ code sample:
DataView dataView = new DataView(dataTable); dataView.RowFilter = String.Format("EventDate > '{0}'", DateTime.Now); dataView.Sort = "EventDate"; dataTable = dataView.ToTable(); while (dataTable.Rows.Count > _rowLimit) { dataTable = dataTable.AsEnumerable().Skip(0).Take(50).CopyToDataTable(); } return dataTable;
If you need only top 15 rows from the database ...its better to use this sql command
select TOP(15) colname from tablename;
this will return top 15 rows and use datatable to fill this result....
The statement
dt.AsEnumerable().Take(15)
given in the question retrieves the first 15 records and returns anIEnumerable
ofDataRow
as explained here
http://msdn.microsoft.com/en-us/library/bb503062.aspx[^]
The issue is not because ofTake(15)
, but because ofCopyToDataTable
method.
CopyToDataTable
hasthree over loads
as explained here
http://msdn.microsoft.com/en-us/library/bb360272[^]
The first overload does not take any parameter and returns aDataTable
.
Whereas theSecond
andThird
overloads take aDataTable
as an argument and copy the rows returned by the query in to thisDataTable
. While doing so theLoadOption
parameter is also must in both the overloads.
So there is no over load ofCopyToDataTable
method which takes only 1 argument. Hence, the error is thrown.
TheLoadOption
parameter gives the options explained here
http://msdn.microsoft.com/en-us/library/system.data.loadoption[^]
So for the query to execute and give the top 15 records either call it as
var dtnew = dt.AsEnumerable().Take(15).CopyToDataTable();
or as
dt.AsEnumerable().Take(15).CopyToDataTable(dtnew,LoadOption.OverwriteChanges);
这篇关于极限数数据表中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!