New Posts New Posts RSS Feed: Devforce Queries and IQueryable<T>.Count()
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Devforce Queries and IQueryable<T>.Count()

 Post Reply Post Reply
Author
jozza View Drop Down
Newbie
Newbie


Joined: 11-Jul-2007
Posts: 25
Post Options Post Options   Quote jozza Quote  Post ReplyReply Direct Link To This Post Topic: Devforce Queries and IQueryable<T>.Count()
    Posted: 25-May-2009 at 6:14pm

Hi,

We are using Devforce 5.1 and I've come across an issue where IQueryable<T>.Count() seems to return only the count of entities that are actually in the database and does not include the ones that have been newly created and are still in the cache.

As an example I have used Northwind and the Product table.

Facade facade = new Facade();

DomainModel.DomainModelEntityManager mgr = new DomainModel.DomainModelEntityManager();
 
int count = facade.GetProducts(mgr).Count<DomainModel.Product>();  <=== This returns 77
 
DomainModel.Product p = DomainModel.Product.Create(mgr, 9000, "Test"); <=== We create a new product
 
count = facade.GetProducts(mgr).Count<DomainModel.Product>(); <=== This still returns 77
 
List<DomainModel.Product> l = facade.GetProducts(mgr).ToList<DomainModel.Product>(); <== We convert it to List
 
count = l.Count<DomainModel.Product>(); <==== Now the count has 78
 
 
The only way I have been able to get the correct count of items in my entity set is to convert the IQueryable results to a List, and then get a count.
 
Is this a bug? or something done by design? If so, what was the rationale behind it?
 
Cheers
 
Jacob Jozwiak
 
 
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 26-May-2009 at 11:03am
This behavior is by design.   An "immediate execution" type of query (such as Count(), Max(), First()) which returns a number or other scalar can only be meaningfully executed against cache or the data source, but not a combination.  In this case if we tried the usual DataSourceThenCache behavior and, for example, found 77 in the database and 78 in cache, what would the union of these two numbers mean?   When the QueryStrategy is "Normal" queries such as this actually resolve to using a DataSourceOnly fetch strategy. 
 
The "Ex03_ExploringFetchStrategies.docx" in the Fundamentals series of Learning Units has a discussion on this topic.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down