I know my question sounds strange but I have a situation in which I have Parent Data which can have 'N' Childs and each child can further have N-Childs and so on.

So its Like.

  1. Parent

    a. Child

        i. Child
        ii. Child

    b. Child


I hope I've made myself Clear. So here is a snapshot of the Database Table:

enter image description here

So I have wrote this Code to get my Data From Database. It is a recursive Function:

public static List<CaseCommunication> GetCaseCommunicationByCustomerCaseId(int customerCaseId)
            var parentCaseCommunications =
                EntityHandler.GetEntitiesContext().CaseCommunications.Where(x => x.CustomerCaseId == customerCaseId && x.ParentCaseCommunicationId == null);

            var mappedParentCommuncations = Mapper.Map<IEnumerable<CaseCommunication>>(parentCaseCommunications).ToList();

            foreach (var parent in mappedParentCommuncations)
                parent.ChildCommunications = GetListOfChildCommunicationsByParentId(parent.CaseCommunicationId);

            return mappedParentCommuncations;

        public static List<CaseCommunication> GetListOfChildCommunicationsByParentId(int caseCommunicationId)
            var childCommunications =
                EntityHandler.GetEntitiesContext().CaseCommunications.Where(x => x.ParentCaseCommunicationId == caseCommunicationId);

            var mappedChild = Mapper.Map<IEnumerable<CaseCommunication>>(childCommunications).ToList();

            foreach (var child in mappedChild)
                child.ChildCommunications = GetListOfChildCommunicationsByParentId(child.CaseCommunicationId);

            return mappedChild;

This is the function called from the other class: GetCaseCommunicationByCustomerCaseId

While GetListOfChildCommunicationsByParentId is the recursive function.

I was just need to know is there a better way of writing this?

Is this efficient?


1 Answer 1


It's not strange at all. It's just a tree, or graph, which is very common.

You don't even need two methods: a single one is sufficient. You should define a single recursive method that takes in a list (or some other collection) of id's and returns those id's and all their children and grand-children, etc. If you want the children of a single element instead of a list of id's, you can just pass a list of one element (or define a method that does that).

Actually, it might be better to use a set instead of list of id's as the argument to the method since it would then handle duplicates and circular references. ALso, if you use a list instead, you would have to specify in the documentation what the ordering of the returned list means.

  • \$\begingroup\$ Do you have an example of such code? Is there a way to make my code efficient? \$\endgroup\$ Commented Nov 8, 2014 at 0:05
  • \$\begingroup\$ No, but just search for "breadth first search" or "depth first search" which are the two usual ways to traverse a tree. \$\endgroup\$
    – toto2
    Commented Nov 8, 2014 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.