Object Relationships

Posts   
 
    
JustinN avatar
JustinN
User
Posts: 11
Joined: 29-Jun-2008
# Posted on: 12-Jul-2008 23:17:23   

Hey all,

Firstly, I am using:

LLBLGen Pro Version 2.6 Final ( June 6th, 2008 ) - Evaluation Copy

I can itterate through my entity collections, no problem at all... however I have no clue how I can itterate through another entity collection, based on a value returned from an entity inside another collected. That probably is hard to understand, let me try demonstrate...

Here is my current code:

EntityCollection<VmmDriverHistoryEntity> drivers = new EntityCollection<VmmDriverHistoryEntity>(new VmmDriverHistoryEntityFactory()); adapter.FetchEntityCollection(drivers, null); foreach (VmmDriverHistoryEntity driver in drivers) { // do something }

Here is pseudo code for what I want to try do:

EntityCollection<VmmDriverHistoryEntity> drivers = new EntityCollection<VmmDriverHistoryEntity>(new VmmDriverHistoryEntityFactory()); adapter.FetchEntityCollection(drivers, null); foreach (VmmDriverHistoryEntity driver in drivers) { EntityCollection<VmmVehicleEntity> vehicles = new EntityCollection<VmmVehicleEntity>(new VmmVehicleEntityFactory()); adapter.FetchEntityCollection(vehicles, null).Where(driver.VehicleID == vehicles.VehicleID); foreach (VmmVehicleEntity vehicle in vehicles) { // do something } }

I hope it was clear what I am trying to do... if you need me to try better explain myself, please just ask. I've had a look around the documentation, but couldn't find exactly what I'm after.

Regards, Justin

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 13-Jul-2008 01:43:33   

Best thing to do is to use prefetchPaths to fetch drivers with their related vehicles in one go, as follows.

EntityCollection<VmmDriverHistoryEntity> drivers = new EntityCollection<VmmDriverHistoryEntity>();

PrefetchPath2 prefetch = new PrefetchPath2((int)EntityType.VmmDriverHistoryEntity);
prefetchPath.Add(VmmDriverHistoryEntity.PrefetchPathVmmVehicle);

adapter.FetchEntityCollection(drivers, null, prefetch);
foreach (VmmDriverHistoryEntity driver in drivers)
{
     foreach (VmmVehicleEntity vehicle in driver.vehicles)
     {
          // do something
     }
}
JustinN avatar
JustinN
User
Posts: 11
Joined: 29-Jun-2008
# Posted on: 13-Jul-2008 11:25:56   

Hi Walaa,

Unfortunately I'm having problems with that code. There is no "vehicles" found within "driver" on this line:

foreach (VmmVehicleEntity vehicle in driver.vehicles) {}

I added the PrefetchPath 2 as you demonstrated, but it didn't give me the results, am I perhaps missing something?

Regards, Justin

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 13-Jul-2008 15:03:14   

My code was just for demonstration, so vehicles can be spelled differently.

Please answer the following questions:

1- In LLBLgen Pro designer -> DriverEntity properties -> "fields mapped on relation" tab: is there a vehicles field?

2- In LLBLgen Pro designer -> DriverEntity properties -> relations tab: can you see the "Vehicles" listed there?

3- Is there a relation between these entities in the LLBLGen Pro Designer?

4- Is there a database relation between Vehicles & Drivers? Is there a FK in the Vehicles table that points to the Driver table?

To use a prefetchPath, your answer should be Yes to all of the above questions.

JustinN avatar
JustinN
User
Posts: 11
Joined: 29-Jun-2008
# Posted on: 13-Jul-2008 15:37:43   

Hi Walaa,

I can answer yes to all 4, and found the name of the object I am after is called "VmmVehicle", however that is a single entity, rather than a collection. Should I be casting it to an IENumerable, or perhaps making a call to a function within it to gain access to the collection, rather than the single entity?

Regards, Justin

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 13-Jul-2008 16:02:49   

Then each driver in VmmDriverHistoryEntity has a relation with only one VmmVehicle. And hence no need for inumeration. am I mkissing something?

JustinN avatar
JustinN
User
Posts: 11
Joined: 29-Jun-2008
# Posted on: 13-Jul-2008 16:06:30   

Sorry Walaa, in some cases one driver may have multiple vehicles, which is why I was expecting a collection.

Regards, Justin

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Jul-2008 20:34:06   

Must be the relation type, it's 1:1 instead of 1:m. You should check the relation type at your DB and at LLBLGenDesigner. Could you check that? If possible, post the tables DDL code.

David Elizondo | LLBLGen Support Team