Print Page | Close Window

Devforce Queries and IQueryable<T>.Count()

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1280
Printed Date: 29-Apr-2025 at 4:53pm


Topic: Devforce Queries and IQueryable<T>.Count()
Posted By: jozza
Subject: Devforce Queries and IQueryable<T>.Count()
Date 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
 
 



Replies:
Posted By: kimj
Date 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.



Print Page | Close Window