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
2010-07-14 05:31:56
+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
2010-07-14 05:33:37
but am getting an error :Filter expression ''Top 5'' does not evaluate to a Boolean term.
sumana shabeeb
2010-07-24 06:23:18
+3
A:
I think, You can use LINQ:
datatable.AsEnumerable().Take(5);
Pavel Belousov
2010-07-14 05:34:27
that's basically an iteration. this is exactly what the querist didn't want!
Andreas Niedermair
2010-07-14 05:36:05
@Andreas Niedermair, as I understand, querist doesn't want to write his own code with iterations.
Pavel Belousov
2010-07-14 05:39:44
@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
2010-07-14 05:40:18
@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
2010-07-14 05:43:12
datatable.AsEnumerable().Take(5); is ok..but i am using VS 2005, this code wont work in it.thats my problem
sumana shabeeb
2010-07-14 05:52:16
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
2010-07-14 07:18:44