Getting Count from Related Table

Posts   
 
    
WooTz
User
Posts: 1
Joined: 20-Nov-2004
# Posted on: 19-Sep-2005 21:25:55   

Hi,

I'm fetching an entity collection from a table using the Adapter Scenario, however I'd also like to get the count of related records, I could also fetch that collection but i'd imagine its a waste of memory. Is it possible to get the first entity collection, and use an aggregate to get the count of the related collection?

Rgds, Martin

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Sep-2005 11:09:37   

I would suggest, creating a view in the database. And then use the Designer to map this view to a typed view, or to an Entity if you want to relate it to other entities.

Using Customers-Orders tables in Northind as an example the database view may look like this:

select Customers.ContactName, RelatedTable.OrdersCount from Customers Inner join ( select CustomerID, Count(OrderID) as OrdersCount from Orders Group By CustomerID ) RelatedTable On RelatedTable.CustomerID = Customers.CustomerID

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 20-Sep-2005 11:18:18   

Additionally: you can't fetch the 'count' of a related table as a field in the parent table. You could fetch that count ONCE of course, by using the GetDbCount feature of the DataAccessAdapter object.

Frans Bouma | Lead developer LLBLGen Pro