Your model makes absolutely no sense.
0) The
MyCondition
property doesn't appear to have any association with the specified question.
1) The ParentID property should be called ID because it's the id of the object, not the parent object..
Assuming EF generates partial entity classes (I don't know because I avoid EF at all costs), create a partial file for the entity and put add a method to your entity that gets it's parent object id, and when you get to an object that doesn't have a parent, return its id
public int GetTopParentID()
{
return (this.ParentObject != null) ? this.ParentObject.GetTopParentID() : this.ParentID;
}
That's the absolute best way to do it (no iteration necessary).