Custom object referencing itself, how to stop it forever?

I have a custom school object that is part of the school family. Therefore, when I return to school, I can return the schools with which it is associated in my family.

However, when I do this, he manages to go in cycles forever, how can I stop him. As soon as 1 level of depth goes and does not become recursive?

public class School
{
    public long BaseId { get; set; }
    public string BaseName { get; set; }

    public string SchoolFamily { get; set; }

    public List<School> LinkedSchools 
    {
        get
        {
            var schoolRepository = new SchoolRepository();
            return schoolRepository.GetAllSchoolsLinkedByFamily(SchoolFamily).ToList();
        }
        set { ; }
    }
}
+3
source share
4 answers

Add a boolean member variable to act like a flag, cleared by default. The first thing to do in the get function is to check this flag. If it is installed, return an empty list. Otherwise, set the flag, create a list and clear the flag, then return the list.

+1
source

() GetAllSchoolsLinkedByFamily, , .

+1

LinkedSchools , nestLevel. - :

public List<School> GetLinkedSchools(int nestLevel)
{
    // Get schools logic here...
}

, , nestLevel.

0

. / , . , , .

. Mark Jones Graph Traversal ( ) , , .

0
source

All Articles