I have following structure:
class Employee
{
public long Id { get; set; }
public long? ParentId { get; set; }
public Employee(long id, long? parentId)
{
Id = id;
Parent_Id = parentId;
}
}
Let’s build some tree structure:
var employees = new List<Employee>();
employees.Add(new Employee(1 , null));
employees.Add(new Employee(2 , 1));
employees.Add(new Employee(3 , 2));
How to check (using C#) if employee with Id=1 is parent of employee with Id=3 within this list? Tree structure could be much more complicated .
To check if is descendant you can traverse up the tree and see if you find him: