title: Paging with single query in Asp.net Web Forms and Entity Framework
body: <p>I have a page that list some contents. I have created a paging for the page with the code below. But as you see i had to create a second query to get RowCount. Can i achive paging without a second query to database?</p>
  
  <pre><code>// ContentList.aspx.cs
  MySiteEntities db = new MySiteEntities();
  
  my_ContentList = db.Content.Where(it =&gt; it.Language == "En" &amp;&amp; it.Type == QueryType &amp;&amp; it.Active == true &amp;&amp; it.Deleted == false).OrderBy(it =&gt; it.Sort).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToList();
  
  RowCount = db.Content.Where(it =&gt; it.Language == "En" &amp;&amp; it.Type == QueryType &amp;&amp; it.Active == true &amp;&amp; it.Deleted == false).Count();
  
  PageCount = RowCount / PageSize + 1;
  
  
  
  
  // ContentList.aspx
  
  &lt;%if (RowCount &gt; PageSize) {%&gt;
  
     &lt;%if (PageIndex &gt; 1) {%&gt;&lt;a href="Content/&lt;%=Type%&gt;/&lt;%=PageIndex-1%&gt;" class="page-numbers prev"&gt;Previous&lt;/a&gt;&lt;%}%&gt;
  
      &lt;%
      int Position = 1;
      do 
      {
      %&gt;
          &lt;a href="Content/&lt;%=Type%&gt;/&lt;%=Position%&gt;" class="page-numbers &lt;%if (Position == PageIndex) {%&gt;current&lt;%}%&gt;"&gt;&lt;%=Position%&gt;&lt;/a&gt;
      &lt;%
      Position++;
      } while (Position &lt; PageCount+1);
      %&gt;
  
     &lt;%if (PageIndex != PageCount) {%&gt;&lt;a href="Content/&lt;%=Type%&gt;/&lt;%=PageIndex+1%&gt;" class="page-numbers next"&gt;Next&lt;/a&gt;&lt;%}%&gt;
  
  &lt;%}%&gt;
  </code></pre>