public void FindParent(List<EntityItem> entityItemsList, ref List<Group> groups)
 {
     //Step 2: Find all children for each parent
     while (entityItemsList.Count > 0)
     {
         var itemsToRemove = new List<EntityItem>();
         foreach (var entityItem in entityItemsList)
         {
             var group = new Group
             {
                 Id = entityItem.Id,
                 GroupName = entityItem.Entity,
                 ChildGroups = new List<Group>()
             };
             foreach (var parent in groups)
             {
                 if (entityItem.Parent == parent.Id)
                 {
                     parent.ChildGroups.Add(group);
                     itemsToRemove.Add(entityItem);
                 }
                 else
                 {
                     if (parent.ChildGroups.Count > 0)
                     {
                         foreach (var child in parent.ChildGroups)
                         {
                             if (child.Id == entityItem.Parent)
                             {
                                 child.ChildGroups.Add(group);
                                 itemsToRemove.Add(entityItem);
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         entityItemsList.RemoveAll(itemsToRemove.Contains);
     }
 }
 public JsonResult GetTree()
 {
     var entityItemsList = NotificationService.GetSMSTree("97005");  //TODO pass ORgID to db
     var groups = new List<Group>();
     //Convert to tree
     //Step 1: Find all parents
     foreach (var entityItem in entityItemsList)
     {
         if (entityItem.Parent == null || String.IsNullOrEmpty(entityItem.Parent))
         {
             var group = new Group
             {
                 Id = entityItem.Id,
                 GroupName = entityItem.Entity,
                 ChildGroups = new List<Group>()
             };
             groups.Add(group);
         }
     }
     entityItemsList.RemoveAll(item => item.Parent == null || String.IsNullOrEmpty(item.Parent));
     FindParent(entityItemsList, ref groups);
     return new JsonResult { Data = groups, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
 }