0

I have written code for getting hierarchical data for the location. I am able to get the hierarchy to TWO level only, but what I'm looking for nth level hierarchy. I have written the recursive function as well, but unable to add the data into the hierarchy.

Data Model Class: -

public class RoomDTO
    {
        public Guid RoomId { get; set; }
        public string Name { get; set; }    

        public Guid? ParentRoomId { get; set; }       
        public List<RoomDTO> Children { get; set; } = new List<RoomDTO>();

    }

Parent Function:-

private List<RoomDTO> RoomData(List<SubLocation> subLocations)
        {
            List<RoomDTO> rooms = new List<RoomDTO>();
            foreach (var location in subLocations.Where(x => x.HasChildLocations))
            {
                var subLocationDTO = new RoomDTO
                {
                     RoomId = location.SubLocationId
                     ,Children = getChild(subLocations, location, new RoomDTO()).Children
                };

                rooms.Add(subLocationDTO);
            }
            return rooms;
        }

Recursive Function: -

private RoomDTO getChild(List<SubLocation> list, SubLocation location, RoomDTO roomDTO)
        {

            var child = list.Where(x => x.ParentSubLocationId == location.SubLocationId).ToList();
            for (int i = 0; i < child.Count; i++)
            {
                var childRoom = new RoomDTO {
                    ParentRoomId = child[i].ParentSubLocationId
                    , RoomId = child[i].SubLocationId
                };                
                roomDTO.Children.Add(childRoom);
                getChild(list, child[i], roomDTO);
            }
            return locList; 
        }

Calling function: -

 public void GetRoomList()
{
            var subLocationList = _context.SubLocations.AsNoTracking().ToList();

            var data = RoomData(subLocationList);

}

Please advise what incorrect I'm doing here.

Arayn
  • 986
  • 1
  • 16
  • 24
  • Can you explain the condition "unable to add the data into the hierarchy"? Are you getting error for current setup or want trying something with new hierarchy? – Tetsuya Yamamoto Oct 17 '18 at 14:36
  • `getChild(subLocations, location, new RoomDTO()).Children` should you really be passing `subLocations`? Something seems funny here - but I do not really understand your code. Also it doesn't seem quite right that a lot of rooms are being created with the children? – BenKoshy Oct 18 '18 at 03:49
  • roomDTO.Children.Add(childRoom); this line is adding children at the same level although they are at a lower level in the hierarchy. A room can have child room(s) and a child room can have further child room(s). – Arayn Oct 18 '18 at 05:40
  • can you put your sublocation class code the way we can understand more what your code is doing ? – Haithem KAROUI Oct 18 '18 at 09:22
  • You can simplify your code by using `.ToLookup` - refer [this answer](https://stackoverflow.com/questions/46560515/how-to-create-dynamic-menu-using-tree/46562343#46562343) for an example –  Oct 24 '18 at 03:28
  • @StephenMuecke, thanks for link. It did solve the problem. I needed to add Order column in table to identify the sequence of child's. – Arayn Oct 25 '18 at 05:00

1 Answers1

0

I followed below steps to resolve the issue, 1> Added Field(SortId) in table to identify the sequence of parent and child items. 2> From list of items, filtered the data based on ParentId and then sorted by SortId

if (summaryList.Count > 0)
            {
                //make Mneu structure 
                foreach (var menu in summaryList.Where(x => x.data.ParentSubLocationId == null).OrderBy(x => x.data.SortId))
                {
                    menu.children = GetChildMenuIetms(menu, summaryList);
                    menus.Add(menu);
                }
            }

3> In above foreach loop, executed recurrsive GetChildMenuItem function to fetch child itmes, as shown below,

public List<Child> GetChildMenuIetms(Child menuItem, List<Child> menuItems)
    {
        List<Child> childerns = null;
        childerns = menuItems.Where(x => x.data.ParentSubLocationId == menuItem.data.SubLocationId).OrderBy(x => x.data.SortId).ToList();

        if (childerns != null && childerns.Count > 0)
        {
            for (int i = 0; i < childerns.Count; i++)
            {

                childerns[i].children = GetChildMenuIetms(childerns[i], menuItems.Where(x => !childerns.Any(c => c.data.SubLocationId == x.data.SubLocationId)).ToList());
            }
        }
        return childerns;
    }
Arayn
  • 986
  • 1
  • 16
  • 24