I am performing "semi"-custom paging for my dynamic LINQ query. However, if the number of results returned exactly fill the page, then I wind up with one extra page that's empty and I can't figure out why. i.e. if I have a page size of 15 and 14, 21, 101, etc. items then everything is fine. However, if I have 15, 30, 45, 60, etc. then I get one extra blank page and I've been unable to make it stop. Anyone have some ideas except to add a new record when I see we're sitting on a multiple of the page size?

I write out the paging control like this and it loops through one time too many if the last page is full...

protected void WritePagingControlHtml(int inputNumberOfPages, int inputCurrentPageNumber)
{
// testing.Text += "<br /> Starting WritePagingControlHtml(int inputNumberOfPages)";

string outputLiteralPageControlHTML = "";
int displayNumberOfPages = inputNumberOfPages + 1;
int displayCurrentPageNumber = inputCurrentPageNumber + 1;
// testing.Text += "<br /> displayNumberOfPages = " + displayNumberOfPages;
// testing.Text += "<br /> displayCurrentPageNumber = " + displayCurrentPageNumber;
for (int loopDisplayPageNumber = 1; loopDisplayPageNumber < displayNumberOfPages + 1; loopDisplayPageNumber++)
{
string outputPagingLinkClass = "";
if (loopDisplayPageNumber == displayCurrentPageNumber)
{
outputPagingLinkClass = " class=\"currentPage\"";
}

// testing.Text += "<br /> Before GetAppLink call: classFilterQueryStringParameter = " + classFilterQueryStringParameter;

outputLiteralPageControlHTML += " <a href=\"" + GetAppLink(classSortQueryStringParameter, classFilterQueryStringParameter, loopDisplayPageNumber) + "\"" + outputPagingLinkClass + ">" + loopDisplayPageNumber.ToString() + "</a>";
}

litPagingControl.Text = outputLiteralPageControlHTML;
}


And I calculate the number of pages as part of the query code...

cmschangeTestDataContext db = new cmschangeTestDataContext();
IQueryable<cmschangesTest> query = db.cmschangesTests;
int newRowCount = 0;

if (inputFilterValue == "")
{
query = db.cmschangesTests
.OrderBy(inputOrderValue)
.Skip(skip)
.Take(take);

newRowCount = db.cmschangesTests.Count();
}
else
{
query = db.cmschangesTests
.Where(inputFilterValue, System.DateTime.Now)
//.Where(a => a.dontpublishbeforedate < System.DateTime.Now) // using lambda expression
.OrderBy(inputOrderValue)
.Skip(skip)
.Take(take);

newRowCount = db.cmschangesTests.Where(inputFilterValue, System.DateTime.Now).Count();
}

testing.Text += " <br /> inputFilterValue = \"" + inputFilterValue + "\"";
testing.Text += " <br /> newRowCount = " + newRowCount.ToString();

int inputNumberOfPages = newRowCount / numberOfContactsPerPage;

WritePagingControlHtml(inputNumberOfPages, currentPageNumber);