Position of an element in a EntityCollection is not fixed?

Posts   
 
    
JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 04-Feb-2009 16:44:53   

Hi,

I'm using 2.6, SelfService.

I noticed something peculiar. Suppose you have a 1:n relation between two entities, e.g. Parent and Child. When I add the Children to the Parent, the entitycollection Parent.Children seems to be in the order the Children are added to the Parent.

When you retrieve a Child from the database directly by its PK the following occurs:

ChildEntity xxx = new ChildEntity(PK).

(xxx.Parent.Child.Last()) == xxx) is allways false

whereas

(xxx.Parent.Child.[0] == xxx) is allways true

What is the easiest way to determine if a child was added last? (My trigger being the PK from the child).

Kind regards,

Jan

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 04-Feb-2009 17:03:06   

I'm nt sure I understand the question.

How do I add the single fetched child to the children collection of the Parent?

JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 04-Feb-2009 17:41:44   

Hi Walaa,

When you retrieve a child from the database and access the parent, the child is allways the first in the collection child.Parent.Child.

When you retrieve the parent from the database and enumerate the children, the children are in the order they were added to the database.

I assumed that for the last child added to the database the equality

child.Parent.Child.Last() == child

would be true. This is not the case.

The question is: is there a simple way to verify that a child is the last one added?

I'm now enumerating thru the Ids and selecting the maximum and verify if that Id is equal to the child id. This works because the Ids are automatically generated.

Jan

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

Hi Jan,

As Walaa, I don't fully understand your problem: Not sure if you want the latest child added to the DB or the latest child added to the parent child's collection, as from your code is not that obvious.

Anyway, as ne enumeration is working for you, why not do this?:

child.Parent.Child[child.Parent.Child.Count-1]
David Elizondo | LLBLGen Support Team
JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 05-Feb-2009 13:32:59   

Daelmo,

I was using your suggested code when I experienced the strange behaviour. It does not work!

I'll try to give an example of what I encountered:

Suppose you have two tables in the database: Parent and Child. Parent can have one-to-may children.

E.g.

Parent: Peter (with PK = 1).

Child: Mary (with PK = 1), Suzy (with PK = 2) and John (with PK = 3).

When you retrieve Peter from the database and enumerate the children, the order of the children in the collection is by PK.

When instead you retrieve a child from the database, and then use code like the one you are suggesting, the following will occur:

for (int PK = 1, PK <= 3, PK++) { ChildEntity child = new ChildEntity(PK); if (child.Parent.Child[0].Id == PK) { Console.WriteLine("PK {0} is first", PK); } } This wil result in

PK 1 is first PK 2 is first PK 3 is first

being shown on the console.

When you inspect the collections of the Parent.Child, the order of the children in the collection depends on the child via which the parent is accessed:

PK 1, then the order is 1, 2, 3 PK 2, then the order is 2, 1, 3 PK 3, then the order is 3, 1, 2

This explains why using the code you suggest does not work properly. Perhaps this behaviour depends on the database being used? I'm using SQLServer 2005.

Hope this clarifies it betterconfused

Jan

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 06-Feb-2009 10:48:23   

I think this happens because of how related entities get associated. The Children collection of a Parent if accessed from a Child will initially contain this child, before fetching the rest of the collection. And since this child already exists in the collection it is not added again to the collection.

Anyway you can always sort the collection in memory after being fetched to insure a specific order of entities.

JayBee
User
Posts: 282
Joined: 28-Dec-2006
# Posted on: 06-Feb-2009 18:54:47   

Hi Walaa,

I realised that myself after thinking it over. Still, it is surprising.

Jan