0

I have to two function which is used to find tags inside the tags like, there is a tag A=B(C(D(E))) so i have to find all the tags inside B then all the tags inside C and so on. I write two function but getting the error System.StackOverflowException. In the first function i am providing the tag ID and against that tag id i am getting getNestesCalTagsId and then calling the getNestedCalTagsIngredients() function. But when there are lot of recursion calls i get the error System.StackOverflowException. Below is my whole code.

  public List<int?> getNestedCalTags(int? calTagId)
        {
            var getNestesCalTagsId = db.Dependencies_Metrix.Where(x => x.Cal_Tag_P_Id == calTagId && x.Status == true && x.Cal_Tag_Id_FK!=null).Select(x => x.Cal_Tag_Id_FK).ToList();
            if (getNestesCalTagsId.Count > 0)
            {
                nestedCalFTags.AddRange(getNestesCalTagsId);
                foreach (var item in getNestesCalTagsId)
                {
                    if (item != null)
                    {
                        getNestedCalTagsIngredients(item.Value);
                    }
                }
            }
            if (nestedCalFTags.Count > 0)
            {
                int countedTags = nestedCalFTags.Count;
                List<int?> tags = new List<int?>(nestedCalFTags);
                for (int i = 0; i < tags.Count; i++)
                {
                    if (tags[i] != null)
                    {
                        getNestedCalTagsIngredients(tags[i].Value);
                    }
                }
            }
            return nestedRawTags;
        }
        public bool getNestedCalTagsIngredients(int nestCalTagId)
        {
            var getCalTags = db.Dependencies_Metrix.Where(x => x.Cal_Tag_P_Id == nestCalTagId && x.Status == true).ToList();
            if (getCalTags.Count > 0)
            {
                foreach (var item in getCalTags)
                {
                    if (item.Cal_Tag_Id_FK != null)
                    {
                        var getNestedCalTagParent = db.Dependencies_Metrix.Where(x => x.Cal_Tag_P_Id == item.Cal_Tag_Id_FK && x.Status == true && x.Cal_Tag_Id_FK!=null).Select(x => x.Cal_Tag_Id_FK).ToList();
                        if (getNestedCalTagParent != null)
                        {
                            nestedCalFTags.AddRange(getNestedCalTagParent);
                            getNestedCalTags(item.Cal_Tag_Id_FK);
                        }
                    }
                    else
                    {
                        var rawTagId = db.Dependencies_Metrix.Where(x => x.Cal_Tag_P_Id == item.Cal_Tag_P_Id && x.Real_Tag_Id_FK!=null).Select(x => x.Real_Tag_Id_FK).ToList();
                        if (rawTagId != null)
                        {
                            foreach (var rawItem in rawTagId)
                            {
                                if (rawItem!=null)
                                {
                                    if (nestedRawTags.IndexOf(rawItem.Value) == -1)
                                    {
                                        nestedRawTags.Add(rawItem.Value);
                                    }
                                } 
                            }  
                        }
                        nestedCalFTags.Remove(nestCalTagId);
                    }
                }
            }
            return true;
        }
  • You could try to replace your recursive approach with an iterative one (see https://stackoverflow.com/a/159777/6459948) – товіаѕ Nov 26 '20 at 12:47
  • @TobiasWürth is there any way i make some changes in this code. – Nadir Abrar Nov 26 '20 at 13:14
  • I would recommend starting with checking that the code does what it should. I.e. that it recurses with the correct parameters. Second I would check that the in data is valid, i.e. not a graph, and that the depth is reasonable. You might for example use a hashSet at the start of the method to check if you have processed the id before. – JonasH Nov 26 '20 at 15:45

0 Answers0