Relation.remove error

Posts   
 
    
Posts: 134
Joined: 04-Mar-2005
# Posted on: 27-May-2005 23:40:19   

I have the following code:

Dim filterBucket As IRelationPredicateBucket
Dim customFilter As IPredicateExpression = New PredicateExpression
Dim shipToRelation As IEntityRelation = MyCustomerEntity.Relations.CdfLineEntityUsingShipCustomerId
filterBucket = customers.GetRelationInfo
filterBucket.Relations.Add(shipToRelation)
filterBucket.Relations.Add(MyCdfLineEntity.Relations.SubprojectCdfLineEntityUsingCdfId)
filterBucket.Relations.Add(MySubprojectCdfLineEntity.Relations.SubprojectEntityUsingSubprojectId).CustomFilter = customFilter
customFilter.Add(PredicateFactory.CompareValue(SubprojectFieldIndex.ProjectId, ComparisonOperator.Equal, Me.Id))

adapter.FetchTypedList(customers.GetFieldsInfo, customers, filterBucket)

filterBucket.Relations.Remove(shipToRelation)
filterBucket.Relations.Add(shipToRelation)

adapter.FetchTypedList(customers.GetFieldsInfo, customers, filterBucket)

The first fetch works, the second returns an error:

Relation at index 4 doesn't contain an entity already added to the FROM clause. Bad alias? 

Is what I'm doing valid?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 28-May-2005 12:53:09   

I pressume between these lines:


filterBucket.Relations.Remove(shipToRelation)
filterBucket.Relations.Add(shipToRelation)

some other code is present? simple_smile

After the Remove call, does the filterBucket.Relations collection have less elements? or does it stay the same? Also after the Add after that, does the collection count change?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 134
Joined: 04-Mar-2005
# Posted on: 28-May-2005 16:46:05   

Otis wrote:

I pressume between these lines:


filterBucket.Relations.Remove(shipToRelation)
filterBucket.Relations.Add(shipToRelation)

some other code is present? simple_smile

Actually, no. There's no other code. I was having problems with removing and then adding another relationship so I removed and added the same relationship and still got the same problem. That's when I figured that it was time to come to you...

Otis wrote:

After the Remove call, does the filterBucket.Relations collection have less elements? or does it stay the same? Also after the Add after that, does the collection count change?

The relations.count reduces by 1 on the remove and increases by 1 on the add as I would expect.

One further item: I think (but can't verify right now) that the relation being removed and re-added was originally the 4th relation, which corresponds to the error message being generated.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-May-2005 09:46:47   

the '4' means it's the 5th element, so first the relations of the typed list, then the relations you add.

Ok, it works, which is logical. then you remove the relation shipToRelation, so MyCdfLineEntity.Relations.SubprojectCdfLineEntityUsingCdfId becomes relation at index 4. Then you add shipToRelation again, which is appended to the list at the end.

The RelationCollection.ToQueryText method walks the relations, and arrives at relation at index 4, MyCdfLineEntity.Relations.SubprojectCdfLineEntityUsingCdfId. As there are already relations processed, only an 'INNER JOIN entity' will be added, not an 'Entity INNER JOIN entity' (or whatever join type). This means that at least one of the entities in the relation has to be present in the pack already. This isn't the case, as the relation which makes this possible, shipToRelation, hasn't been processed yet as it is the last relation in the list.

This 'ordering' of relations is important, as LEFT/RIGHT joins are supported and these require an order.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 134
Joined: 04-Mar-2005
# Posted on: 31-May-2005 14:39:00   

So the short answer is "Don't use Relations.Add, dummy, use Relations.Insert"?

flushed

Thanks Frans