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.
Parent
a. Child
i. Child ii. Child
b. Child
i.Child
I hope I've made myself Clear. So here is a snapshot of the Database Table:
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?