Counts on relationships

Posts   
 
    
asmith
User
Posts: 27
Joined: 01-Oct-2010
# Posted on: 13-Mar-2013 14:23:37   

I'm running into a problem I can't seem to find the right direction for.

I would like to find the counts on relationships.

I have a table-- let's say "garage" and it has a relationship to vehicles. It also has a relationship to Mehcanics.

I would like to show a nice list of my garages, their name, the number of vehicles and the number of mechanics --

In SQL I might do something like this:

select garage.name, (select count(id) from mechanics where mechanic.garageid=garage.gargageid) MechanicCount, (select count(id) from vehicles where vehicle.garageid=garage.gargageid) VehicleCount from garage

Do I need to put this in a view? Or is there some way for it to be accomplished with LLBLGen "fields mapped onto related fields"? -- Without causing a seperate "round trip" back for each garage.

I feel like I am missing something basic here.

I'm using LLBLGen 3.5 (1/17/2013), C# .net 4.0

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Mar-2013 18:10:51   

There are different ways to accomplish this.

One of them is to Garages with prefetchPaths to Mechanics and Cars. Then you might manually add 2 properties to the Garage entities, which return the count of each of Mechanis and Cars collections.

Another approaches is to use a DynamicList to build the resultSet you need to fetch in a flat manner.

asmith
User
Posts: 27
Joined: 01-Oct-2010
# Posted on: 13-Mar-2013 18:44:54   

So I am adding two properties in code to have the counts-- correct? This may or may not cause a query for each line to be ran? -- Not sure on that part.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Mar-2013 15:56:21   

Nope, prefetchPaths executes one query only per branch.

For example, if you are fetching UK Customers, and you want to fetch their Orders. One query would be executed to fetch Customers filtered by Country == UK. Then another query will be executed to get all Orders related to the previously fetched customers.

In total only 2 queries were executed.