tags:

views:

199

answers:

3

Hi is there any way to select top 5 rows from a data table without iteration?

+1  A: 

why not adapt the underlaying select you are building your dataTable/dataSet with? otherwise give google a try :)

everything else would be an iteration...

Andreas Niedermair
+6  A: 

If you use a LINQ statement, you could use the Take() method.

This post may be of some assistance as well.

EDIT

As you are using VS2005, use the SELECT() method in the datatable like so:

DataRow[] rows = datatable.Select('TOP 5');
Ardman
but am getting an error :Filter expression ''Top 5'' does not evaluate to a Boolean term.
sumana shabeeb
+3  A: 

I think, You can use LINQ:

datatable.AsEnumerable().Take(5);
Pavel Belousov
that's basically an iteration. this is exactly what the querist didn't want!
Andreas Niedermair
+1 Beat me to it. ;)
jrista
@Andreas Niedermair, as I understand, querist doesn't want to write his own code with iterations.
Pavel Belousov
@Andreas: Technically speaking, calling `.AsEnumerable().Take(5)` is **not** an iteration...yet. However, one way or another, an iteration will be required to enumerate the data represented by that query...so I don't fully understand the OP's goal.
jrista
@belousov: that's why i did not downvote :)@jrista: calling a method is no iteration, but it will end up in an iteration...
Andreas Niedermair
datatable.AsEnumerable().Take(5); is ok..but i am using VS 2005, this code wont work in it.thats my problem
sumana shabeeb
there ain't direct way in .net 2.0have a look at this solution http://www.eggheadcafe.com/community/aspnet/2/15478/select-top-10-from-a-datatable.aspx
Sandy