极限数数据表中的行数 [英] limit no. of rows in a datatable

查看:92
本文介绍了极限数数据表中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我有一个正在检索所有记录的数据表dt,但是我只需要前15名,所以我怎么能限制无.数据表中的记录数.请帮助我尝试了一些
代码

 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.
SecondThird 重载将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 an IEnumerable of DataRow as explained here
http://msdn.microsoft.com/en-us/library/bb503062.aspx[^]

The issue is not because of Take(15), but because of CopyToDataTable method.

CopyToDataTable has three over loads as explained here
http://msdn.microsoft.com/en-us/library/bb360272[^]

The first overload does not take any parameter and returns a DataTable.
Whereas the Second and Third overloads take a DataTable as an argument and copy the rows returned by the query in to this DataTable. While doing so the LoadOption parameter is also must in both the overloads.
So there is no over load of CopyToDataTable method which takes only 1 argument. Hence, the error is thrown.

The LoadOption 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆