Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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!
Posted
Updated 13-May-12 21:59pm
v2

You can try using the below LInQ code sample:

C#
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;
 
Share this answer
 
If you need only top 15 rows from the database ...its better to use this sql command

SQL
select TOP(15) colname from tablename;


this will return top 15 rows and use datatable to fill this result....
 
Share this answer
 
v3
Comments
Maciej Los 14-May-12 16:02pm    
Good answer, my 5!
The statement
C#
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
C#
var dtnew = dt.AsEnumerable().Take(15).CopyToDataTable();

or as
C#
dt.AsEnumerable().Take(15).CopyToDataTable(dtnew,LoadOption.OverwriteChanges);
 
Share this answer
 
v2
Comments
Mohammad A Rahman 14-May-12 20:03pm    
good answer :)
VJ Reddy 14-May-12 20:32pm    
Thank you, Rahman :)
VJ Reddy 14-May-12 20:37pm    
Thank you, Rahman.
You can use the default method without arguments in it.

C#
DataTable dtnew = dt.AsEnumerable().Take(15).CopyToDataTable<datarow>();</datarow>


but why you are limiting it on front end? you should do this in database itself in the stored procedure or query you are firing.
 
Share this answer
 
v2
Hi,

Try this:

C#
dt.Rows.Cast<system.data.datarow>().Take(n)
//n is the no of rows.



All the best.
-AK
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900