DynamicRelation pre v2.6

Posts   
 
    
_johnnyboy
User
Posts: 12
Joined: 07-May-2009
# Posted on: 01-Jul-2009 06:50:31   

Hello,

I've utilized the DynamicRelation type in previous solutions using LLBLGen version 2.6, however, I am currently working on a project that is only running v2.0 - is there any method in v2.0 that is similar to dynamic relations?

Cheers

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 01-Jul-2009 10:20:16   

Would you please explain in more details what are you trying to do. Give a code/SQL example.

Joining to a derived table wasn't possible before the introduction of the DynamicRelation/DerivedTables Definition.

But some queries can be re-written to avoid joining to a temp/derived table.

_johnnyboy
User
Posts: 12
Joined: 07-May-2009
# Posted on: 02-Jul-2009 01:35:10   

It's an update query and one of the conditions is based on a value in another table - I cannot have an actual relation between the two tables because the join condition is based on multiple fields.

Using DynamicRelation:


IRelationPredicateBucket filter;
FirstEntity entityA;

filter = new RelationPredicateBucket();

filter.PredicateExpression.Add(FirstFields.Id == id);

// add a dynamic relation to the content entity
filter.Relations.Add(
    new DynamicRelation(
        EntityType.FirstEntity,
        JoinHint.Inner,
        EntityType.SecondEntity,
        null,
        null,
        (
            FirstFields.FieldId == SecondFields.FieldId & 
            SecondFields.AnotherFieldId == 1 & 
            SecondFields.Active == true
        )
    )
);

filter.PredicateExpression.Add(SecondFields.AuthorId == authorID);

entityA = new FirstEntity();
entityA.Deleted = true;
entityA.IsNew = false;

return (UpdateEntitiesDirectly(entityA, filter) > 0);

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 02-Jul-2009 09:41:56   

I think it can be re-written as follows:

filter.Relations.Add(FirstEntity.Relations.SecondEntity);

filter.PredicateExpression.Add(SecondFields.AnotherFieldId == 1); filter.PredicateExpression.Add(SecondFields.Active == true); filter.PredicateExpression.Add(SecondFields.AuthorId == authorID);

If the rleation between FirstEntity and SecondEntity is not defined in the database, you can either define it in the Designer, or define it in code. e.g.

filter.Relations.Add(
    new EntityRelation(
        FirstFields.FieldId,
        SecondFields.FieldId,
        RelationType.OneToMany,
    )
);
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 08-Sep-2009 17:39:58   

Ok. How do I use DynamicRelation in code only to get a join of two entities. I am able to join my entities but I only can get the fields for only one side of the join.

HelperClasses.EntityCollection<TInvEntity> allInventory = new EntityCollection<TInvEntity>(new TInvEntityFactory());

DynamicRelation relation = new DynamicRelation(EntityType.TInvEntity, JoinHint.Left, EntityType.TInvLinksEntity, String.Empty, "LinkEntity", HelperClasses.TInvFields.Id == HelperClasses.TInvLinksFields.MasterInvId.SetObjectAlias("LinkEntity"));

        RelationPredicateBucket filter = new RelationPredicateBucket();
        filter.Relations.Add(relation);
        filter.SelectListAlias="LinkEntity";

        daCDBSETUP.FetchEntityCollection(allInventory, filter);

So this is just a simple join between an Inventory and InventoryDetail Entity. It gives me the correct amount of elements returned in the collection, but the only fields I can access of the ones from the TInvEntity entity and none from the TInvLinksEntity. What am I messing? Do you have an example of joining two in memory entities, and being able to access all the fields after the join? I do now want to do the linking in the designer, I want to know how to do it just using DynamicRelation in code only.

Thanks,

Charlie J.

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 09-Sep-2009 12:16:45   

Joining to another entity has nothing to do with the fields returned in the result set, you are fetching an EntityCollection so you'd only get fields from this entity. If you want to fetch fields from more than one table, then you should use DynamicList instead of the EntityCollection.

cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 09-Sep-2009 23:15:48   

I see. Thanks. I thought the Dynamic Relation was doing the join and regenerating a result set from the join.

Thanks,

Charlie J.

cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 10-Sep-2009 00:26:40   

Thanks for that information. But the two Entities are not related in the database. I know I could add a virtual relation using the designer, but I would like to know how to do it all in code for Two Entities that are not related in the database. So they is what I thought DynamicRelation was suppose to do. So can I do the following and how?

1.) Make two Entities related in code (They are not related in the database or using the designer).

2.) Use PrefetchPath on the Dynamic relationship from Step #1

3.) Get a custom resultset containing both Entities

Thanks,

Charlie J.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Sep-2009 03:06:14   
David Elizondo | LLBLGen Support Team
cjbiggs
User
Posts: 64
Joined: 17-Apr-2009
# Posted on: 10-Sep-2009 17:43:45   

Great. Thanks.