title: Dapper - Multi Mapping with a single return value
body: <p>Below is the code I'm using to return a paged list of objects:</p>
  
  <pre><code>string query2 = @"
          select count(*) as TotalCount from blogposts p where p.Deleted = 0 and p.PublishDate &lt;= @date
          select * from (
              select p.*, 
              row_number() over(order by publishdate desc) as rownum
              from blogposts as p
              where p.Deleted = 0 and p.PublishDate &lt;= @date
          ) seq
          where seq.rownum between @x and @y";
  
  using (var cn = new SqlConnection(connectionString))
  {
      cn.Open();
      using (var multi = cn.QueryMultiple(query2, new { x= lower, y = upper, date = DateTime.UtcNow }))
      {
          var totalCount = multi.Read&lt;int&gt;().Single();
          var posts = multi.Read&lt;PostModel&gt;().ToList();
          return new PagedList&lt;PostModel&gt;(posts, page, pageSize, x =&gt; totalCount);
      }
  }
  </code></pre>
  
  <p>Although this works, it means that I have to define my criteria twice, once for the count query and once for the resultset query. Rather than resort to string concatenation, I can execute just one query:</p>
  
  <pre><code>        string query = @"
                  select * from (select p.*, 
                  row_number() over(order by publishdate desc) as rownum,
                  count(*) over() as TotalCount
                  from blogposts as p) seq
                  where seq.rownum between @x and @y";
  </code></pre>
  
  <p>However, I don't seem able to map this using Dapper. I can't use the same method as above because there aren't multiple results. I've tried using multi mapping but this expects to return an IEnumerable.</p>
  
  <p>How would I map to the following?</p>
  
  <pre><code>    public class PostList
      {
          public IEnumerable&lt;PostModel&gt; Posts;
          public int TotalCount { get; set; }
      }
  </code></pre>
  
  <p>Thanks</p>
  
  <p>Ben</p>