Reading 2 joined tables

Posts   
 
    
mouradb
User
Posts: 1
Joined: 19-Jun-2009
# Posted on: 19-Jun-2009 20:56:08   

I am new to LLBLGen and my first excercise at try to read/traverse 2 joined tables has been so far a frustrating experience.

In simple terms, I have 2 tables (SQL Server - Adpater - 2 classes. Using v2.6) that have 1:1 relationship that I can read with the following SQL statement:

select h.CareFltyId, h.Name, e.FunctDay1 from hzCareFlty h join eqCareFlty e ON (h.CareFltyId = e.CareFltyID)

In others words, I want to read 2 fields from the first table and 1 field from the 2nd table. No filtering and no sorting.

After literally spending hours reading the documentation and browsing this forum, I do not have yet a solution that works. I came very close several times but (for some reason or another), I cannot get my equivalent code to work (either syntax errors or runtime exceptions).

I think the documenation is doing a poor work of explaining something as basic. Trying to use the code samples becomes a challenge in itself since their are few nuances between the different version of LLBLGen, the 2 classes output (should I use <Entity...> or <MyEntity...>, the .net 2.0 syntax vs. older syntax, and in a few times methods are not even there!!

Can someone please provide some sample code?

Thanks,

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Jun-2009 07:28:28   

Hi mouradb,

It's indeed easy. Sorry for the inconvenience but the more you get familiar with LLBLGen syntax the more you will love it.

The simplest code snippet:

// collection to fetch
EntityCollection<HzCareFltyEntity> careFltys = new  EntityCollection<HzCareFltyEntity>();

// specify the prefetch path
IPrefetchPath2 path = new PrefetchPath2((int) EntityType.HzCarFltyEntity);
path.Add(HzCareFltyEntity.PrefetchPathEqCareFlty);

// fetch the collection
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
     adapter.FetchEntityCollection(careFltys, null, path);
}

// ready.. .now acces the entity objects
foreach(HzCareFltyEntity hzCare in careFltys)
{
     Console.WriteLine(hzCare.SomeField);
     Console.WriteLine(hzCare.EqCareFlty.SomeField);
}

Above code is an approximation, but you get the idea. You use prefetchPath when you want to load related objects to the main object. Then you can access the related object and its properties. You have more options but I don't want to overwhelm you with a bunch of things.

Give it a spin. If you have troubles, come back and we will help you wink

David Elizondo | LLBLGen Support Team