I have a class that tracks relations between owner classes. The tree can potentially be infinite--in the DB, each record has a parent ID that is a self-reference to the same table. The class basically looks like this:
public class ObjectRelation
{
public ObjectRelation(GetObjectParentChildList_Result relation)
{
this.ObjectId = relation.Object_ID;
this.ParentObjectId = relation.Parent_Object_ID;
this.ChildObjects = new List<ObjectRelation>();
}
public int ObjectId { get; set; }
public int? ParentObjectId { get; set; }
public List<ObjectRelation> ChildObjects { get; set; }
}
I'd like a way given a reference to a single instance of this class to end up with a list of every unique ID in the tree in one list to ensure that as a user is entering data they don't create a infinite parent/child loop (ie, IDs 1 & 2 being parents of each other) and it looks as though SelectMany is the way to go. Is such a query feasible in LINQ, or am I stuck writing a separate method to recurse down the whole tree and return the calculated list of IDs once I run out of child nodes?