How to fetch records from more than one table?

Posts   
 
    
Posts: 12
Joined: 05-Apr-2006
# Posted on: 11-Apr-2006 14:17:59   

Hi Everyone,

I tried the following code in "UserEntity" Class:

public static HelperClasses.EntityCollection GetUserByEmail(int userId) { EntityCollection col = new EntityCollection(new UserEntityFactory()); DataAccessAdapter da = new DataAccessAdapter();

        RelationPredicateBucket b = new RelationPredicateBucket();
        IPredicateExpression filter = new PredicateExpression();
        filter.Add(PredicateFactory.CompareValue(UserFieldIndex.userId,   
                       ComparisonOperator.Equal, userId));
        b.PredicateExpression.Add(filter);
        da.FetchEntityCollection(col, b);
        return col; 

}

Here, based on the userId passed to the fuction, it retrieves the corresponding row from the "User" table.

But i need to access another table "UserAddress" to get the address for the user.

So based on the UserId i need to retrieve.

User Table contains - userId, userName columns. UserAddress Table contains - userId, Address1,Address2 columns.

Plz help to solve this issue.

Thanks in Advance, Dhivya.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2006 15:29:15   

Please refer to PrefetchPaths in the LLBLGen Pro documentation manual.

"Using the generated code -> Adapter -> Prefetch paths"

Posts: 12
Joined: 05-Apr-2006
# Posted on: 12-Apr-2006 09:23:51   

Hi Walaa,

I refered to the documentation for PrefetchPaths,

My code is here:

EntityCollection users = new EntityCollection(new UserEntityFactory()); IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.UserEntity); prefetchPath.Add(UserEntity.PrefetchPathUserAddress); IRelationPredicateBucket filter = new RelationPredicateBucket(); filter.PredicateExpression.Add( PredicateFactory.CompareValue(UserFieldIndex.userId, ComparisonOperator.Equal, 101)); DataAccessAdapter adapter = new DataAccessAdapter(); adapter.FetchEntityCollection(users, filter, prefetchPath);

I read in the documentation that, the above code is used to fetch the fields from User table and UserAddress table.

But this collection("users ") holds only the fields from User table. How could i extract the fields of UserAddressTable also?

Now i could get the result as:

userId userName

I need the result to be:

userId userName Address1 Address2

Plz help me.

Thanks in Advance, Dhivya.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Apr-2006 16:24:07   

ok first the code you posted will give you the following: User.UserID User.UserName . . User.Address.Address1 User.Address.Address2 . .

this would be fine unless you want to bind to a datagrid for example

As if you set the datasource to Users collection, the User.Address.Address1 wouldn't be bindable.

To work around this, go to the LLBLGen Pro Designer, open the properties of the User Entity, go to the Fields mapped on related fields sub tab and add the Address1 and Address2 to the UserEntity.

Then using the code you posted (with the prefetchPaths), you will get the folowing: User.UserID User.UserName . . User.Address1 User.Address2

Posts: 12
Joined: 05-Apr-2006
# Posted on: 12-Apr-2006 17:00:31   

Hi Walaa,

Thanks a lot for ur response. I set the Fields Mapped on Related fields, and now i could retrieve. Thanks again.

-Dhivya.