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?
// ContentList.aspx.cs
MySiteEntities db = new MySiteEntities();
my_ContentList = db.Content.Where(it => it.Language == "En" && it.Type == QueryType && it.Active == true && it.Deleted == false).OrderBy(it => it.Sort).Skip(PageSize * (PageIndex - 1)).Take(PageSize).ToList();
RowCount = db.Content.Where(it => it.Language == "En" && it.Type == QueryType && it.Active == true && it.Deleted == false).Count();
PageCount = RowCount / PageSize + 1;
// ContentList.aspx
<%if (RowCount > PageSize) {%>
<%if (PageIndex > 1) {%><a href="Content/<%=Type%>/<%=PageIndex-1%>" class="page-numbers prev">Previous</a><%}%>
<%
int Position = 1;
do
{
%>
<a href="Content/<%=Type%>/<%=Position%>" class="page-numbers <%if (Position == PageIndex) {%>current<%}%>"><%=Position%></a>
<%
Position++;
} while (Position < PageCount+1);
%>
<%if (PageIndex != PageCount) {%><a href="Content/<%=Type%>/<%=PageIndex+1%>" class="page-numbers next">Next</a><%}%>
<%}%>