want to be able to limit the number of results to 5. I know that you can alter the SQL query in some versions is this possible is web matrix? otherwise i understan the foreach loop would have to be changed to say a for, while loop.
How would I limit the number of results returned to 5 and do the <tr>....</tr> part?
daza93
Member
5 Points
7 Posts
Limiting the number of results in webmatrix
Mar 03, 2012 06:51 PM|LINK
Hi, I have the following piece of code
<tbody> @foreach (var row in db.Query(selectQueryString)){ <tr> <td><a href="ArtistDescription.cshtml?ID=@row.artistID">@row.showname</a></td> <td>@string.Format("{0:d}",row.showdate)</td> <td>@row.showtime</td> <td>@row.type</td> <td><a href="book.cshtml?showID=@row.showID">Book Now!</a></td> </tr> }want to be able to limit the number of results to 5. I know that you can alter the SQL query in some versions is this possible is web matrix? otherwise i understan the foreach loop would have to be changed to say a for, while loop.
How would I limit the number of results returned to 5 and do the <tr>....</tr> part?
GmGregori
Contributor
5414 Points
728 Posts
Re: Limiting the number of results in webmatrix
Mar 03, 2012 07:09 PM|LINK
In SQL Server you can limit the numer of rows returned by a SQL query with the TOP clause.
The syntax is
where x is the number of row you need.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Limiting the number of results in webmatrix
Mar 04, 2012 07:57 AM|LINK
You can use OFFSET and FETCH in SQL Compact 4.0: http://www.mikesdotnetting.com/Article/150/Web-Pages-Efficient-Paging-Without-The-WebGrid
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
kiruba.sanka...
Member
543 Points
113 Posts
Re: Limiting the number of results in webmatrix
Mar 04, 2012 03:07 PM|LINK
<tbody> @foreach (var row in db.Query(selectQueryString).Top(5)){ <tr> <td><a href="ArtistDescription.cshtml?ID=@row.artistID">@row.showname</a></td> <td>@string.Format("{0:d}",row.showdate)</td> <td>@row.showtime</td> <td>@row.type</td> <td><a href="book.cshtml?showID=@row.showID">Book Now!</a></td> </tr> }-Good Luck
Kiruba.
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Limiting the number of results in webmatrix
Mar 04, 2012 03:54 PM|LINK
I don't think there is a Top() extension method. Perhaps you mean Take?
@foreach (var row in db.Query(selectQueryString).Take(5)){Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter