Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper ORM paging and sorting

Tags:

orm

vb.net

dapper

I am giving the Dapper ORM a try. I am able to query data from a table using the code below:

Dim comments As List(Of Comment)
Using conn = New SqlConnection(ConnectionString)
    conn.Open()
    comments = conn.Query(Of Comment)("SELECT * from comments where userid = @commentid", New With {.userid= 1})
End Using

Return View(comments)

I am interested to learn how to do paging/sorting using Dapper. EF has "skip" and "take" to help with this. I understand that a micro ORM does not have this built in but would like to know the best way to accomplish this.

like image 661
ericdc Avatar asked May 07 '11 00:05

ericdc


People also ask

Is Dapper an ORM?

Dapper is an object–relational mapping (ORM) product for the Microsoft . NET platform: it provides a framework for mapping an object-oriented domain model to a traditional relational database. Its purpose is to relieve the developer from a significant portion of relational data persistence-related programming tasks.

Is Dapper a micro ORM?

This site is for developers who want to learn how to use Dapper - the micro ORM produced by the people behind Stack Overflow.

Why Dapper is micro ORM?

Dapper is an example of Micro ORM, in fact, it is called the King of Micro ORM because of its speed and ease of work. First, it creates an IDbConnection object and allows us to write queries to perform CRUD operations on the database.

Does dapper use SqlClient?

To use Dapper, we first need a DbConnection implementation. In this example, we'll be using System. Data. SqlClient and SqlConnection , but Dapper supports other databases that use the DbConnection abstraction.


1 Answers

If you want to do skip and take with Dapper, you do it with T-SQL.

SELECT *
FROM
(
SELECT tbl.*, ROW_NUMBER() OVER (ORDER BY ID) rownum
FROM comments as tbl
) seq
 WHERE seq.rownum BETWEEN @x AND @y
 AND userid = @commentid
 ORDER BY seq.rownum
like image 151
mattmc3 Avatar answered Oct 19 '22 00:10

mattmc3