What is the QuerySpec equivalent of GetDbCount()?

Posts   
 
    
trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 09-Jun-2012 14:44:43   

I used to use the GetDbCount() function of the adapter to retrieve a "VirtualRecordCount" for a query in order to prepare the paging controls. It was pretty straight forward as I would just pass it the same IRelationPredicateBucket that I would pass FetchEntityCollection.

I can't quite work out how I would do that using QuerySpec.

Here is a sample of the old code (version 2.6):


            // Entity collection that will hold the query result
            var tickerItems = new EntityCollection<NewsTickerItemEntity>();

            if (pagingOptions == null)
                pagingOptions = new DataPagerOptions(0, false);

            // Execute the query against the data store
            Adapter.FetchEntityCollection(tickerItems, bucket, maxRecords, sortExpression, prefetchPath, includeExcludeFields,
                                          pagingOptions.CurrentPage, pagingOptions.PageSize);

            if (pagingOptions.SetVirtualCount)
            {
                var virtualCount = Adapter.GetDbCount(tickerItems, bucket);
                PagingData = new DataPager(virtualCount, pagingOptions);
            }

            return tickerItems;

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Jun-2012 00:22:41   

If you already have a query 'q' you just want to "count", then just do this:

using (var adapter = new DataAccessAdapter())
{
    count = adapter.FetchScalar<int>(qf.Create().Select(q.CountRow()));
}
David Elizondo | LLBLGen Support Team
trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 11-Jun-2012 14:48:55   

Interesting, I'll give that a try. I've had to move away from QuerySpec for the moment because I can't include specific fields without 'q' becoming a DynamicQuery instead of a EntityQuery<T>.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 11-Jun-2012 15:16:41   

trancehead wrote:

Interesting, I'll give that a try. I've had to move away from QuerySpec for the moment because I can't include specific fields without 'q' becoming a DynamicQuery instead of a EntityQuery<T>.

Could you give an example why that would be a problem? I.e. if you want to fetch all Order fields + 3 Customer fields, that obviously doesn't fit in an orderentity (so you need a custom projection), hence the Dynamicquery requirement.

Frans Bouma | Lead developer LLBLGen Pro
trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 11-Jun-2012 15:23:10   

I want to fetch a collection of NewsEntity but I only need the title and the date (not the other fields).

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Jun-2012 20:07:59   

I'm not sure whether you want to fetch specific fields from more than one Entity. Or you want to fetch specific fields from one Entity.

i.e. specific fields from 2 tables, or from a single table.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Jun-2012 07:06:57   

My two cents added to what everyone said: If you want include just some fields or exclude some others, you can use QuerySpec's Include/Exclude functions:

// include relevant fields
var q = qf.Order
    .Include(OrderFields.OrderId, OrderFields.OrderDate);

// exclude expensive fields
var q = qf.Employee
    .Exclude(EmployeeFields.Photo);
David Elizondo | LLBLGen Support Team
trancehead
User
Posts: 137
Joined: 03-Dec-2007
# Posted on: 12-Jun-2012 07:43:00   

@daelmo I thought the include/exclude methods were for something else entirely. That makes a lot more sense now, thanks.