2

I have a class that contains a reference to a parent object

class Joint 
{
    public Joint Parent { get; private set; }
}

Given a single Joint I can enumerate all the parent objects in the chain with

    public IEnumerable<Joint> ThisAndParentJoints
    {
        get
        {
            Joint joint=this;
            while(joint!=null)
            {
                yield return joint;
                joint=joint.Parent;
            }
        }
    }

Is there a way to do the above using a single LINQ statement?

NOTE: I can find all the child joints given a List<Joint> all

public IEnumerable<Joint> ChildJoints { 
    get { return all.Where((jnt) => jnt.Parent==this); } 
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • 1
    Thanks, although the recursion works the other way in the other post, indeed it answers my question and it is similar enough to be makred as a duplicate. – John Alexiou Jan 16 '15 at 22:44

0 Answers0