Print Page | Close Window

Skip, Take and Count

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2922
Printed Date: 12-Apr-2026 at 5:55am


Topic: Skip, Take and Count
Posted By: mickan
Subject: Skip, Take and Count
Date Posted: 24-Aug-2011 at 7:26am
From what I've read on the board I think I understand that the linq Count() operation causes immediate execution of the query.  So if the query was to return thousands of rows that would be "unfortunate" for the user, especially over a wan.
 
I can use Skip() and Take() to chunk the data retrieval, but my Xceed grid wants a count of possible records, presumably to display the thumb slider at an appropriate size and do other housekeeping for "virtualizing" the data presentation.
 
So my questions are:
 
1. Are skip() and take() executed in the BOS so that only the requested rows get transferred from the data source?
2. Is requesting an implementation of a linq extension, say ToCount(), a reasonable ask of the DevForce linq provider?
3. Any better suggestions for me than having two methods for potentially large queries in my repository, such as
 
IList GetLotsOfEntities();
int    GetLotsOfEntitiesCount();



Replies:
Posted By: sbelini
Date Posted: 25-Aug-2011 at 10:53am
Hi mickan,
 
Yes, Count() causes immediate execution of the query, but that doesn't mean that all the records have been returned to the client.
 
You can verify that:
 
  mgr.Clear();
  var ordersCount = mgr.Orders.Count(); // will return the number of Orders in the datasource
  var orders = mgr.Orders.With(QueryStrategy.CacheOnly).Execute(); // will be empty because no Orders were loaded in the client
 
or, if working asynchronously:
 
  mgr.Clear();
  var ordersCountOp = mgr.Orders.AsScalarAsync().Count();
  ordersCountOp.Completed += (o, args) => {
    var ordersCount = ordersCountOp.Result; // will return the number of Orders in the datasource
    var orders = mgr.Orders.With(QueryStrategy.CacheOnly).Execute(); // will be empty because no Orders were loaded in the client
  };
 
To answer your questions:
1. Only the requested rows will be transfered from the data source.
2. Not necessary. (given explanation about Count() above)
3. If you are retriving a really large number of records and it's causing your app to 'hang' during this retrieval, I'd suggest issuing 2 queries: one for the records that will populate the first grid page and other for all records.
 
Regards,
   Silvio.


Posted By: mickan
Date Posted: 25-Aug-2011 at 11:13am
Silvio, thanks thats very helpful, especially the code sample.   To dig a little deeper:
 
Do projections behave the same way, or will they result in data row transfer?
With a BOS involved, will there be data row retrival into the BOS in either of the entity or projection cases when returning a count?


Posted By: sbelini
Date Posted: 25-Aug-2011 at 12:05pm
Mickan,
 
Projections will return only the entity part you selected (not the entire entity). There is an article in the http://drc.ideablade.com/xwiki/bin/view/Documentation/query-anonymous-projections - DevForce Resource Center that explains this topic in detail.
 
When working n-tier, no entities will be loaded into the BOS when returning a count.
 
Silvio.



Print Page | Close Window