Best way to get total count when using paging?

Posts   
 
    
Posts: 34
Joined: 08-Nov-2007
# Posted on: 21-Dec-2007 16:57:10   

Do I have to run the query twice? I searched but not sure how relevant to 2.5 Adapter.

 public MySpecialObject GetAllPeopleByPage(int itemsPerPage, int pageNumber, Dictionary<string, string> searchBucket)
    {

        EntityCollection<PersonEntity> people = new EntityCollection<PersonEntity>();



        IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.PersonEntity);
        prefetchPath.Add(PersonEntity.PrefetchPathCompanyPerson).SubPath.Add(CompanyPersonEntity.PrefetchPathCompany);


        IRelationPredicateBucket relPred = new RelationPredicateBucket();

        foreach (string searchKey in searchBucket.Keys)
        {

            string searchValue = searchBucket[searchKey];

            if (searchKey.Equals("Surname"))
            {
                relPred.PredicateExpression.Add(PersonFields.Surname % searchValue);
            }

            if (searchKey.Equals("FirstName"))
            {
                relPred.PredicateExpression.Add(PersonFields.FirstName % searchValue);
            }



        }

        Adapter.FetchEntityCollection(people, relPred, -1, null, prefetchPath, pageNumber, itemsPerPage);

MySpecialObject currentPage = new MySpecialObject(); currentPage.People = people currentPage.TotalAvailableRecords = ???? (ignore paging)?? return currentPage;

    }

public class MySpecialObject {

// YES PSEDUO I WOULD USE PROPERTIES simple_smile public List<PersonEntity> People; public int TotalAvailableRecords;

}

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 21-Dec-2007 17:15:40   

yes you will need to execute 2 queries. 1 to get your page of data 1 to get the total number of rows.

it's simplest form looks like this

public EntityCollection<MyEntity> FetchAPage(pageIndex)
{
     EntityCollection<MyEntity> myCollection = new EntityCollection<MyEntity>();
     adapter.FetchEntityCollection(myCollection, ...., pageIndex + 1, 10);
     return myCollection;
}

public int FetchTheTotalNumberOfRecords()
{
     return adapter.GetDbCount(myCollection, null);
}

I pass the page index around until the last possible moment then +1 to calculate the page number. just a personal prefference. of course you can also pass make the page size a parameter too.