C#C
C#11mo ago
Alex Frost

Issue sending parent Id through recursive function

I have a function that goes through provided nested enumerable with varying depth.
It returns element that pass the given condition.
I'm having issue with child elements to have I'd of the parent. For that, I am trying to pass Id of found valid element through the preceding recursion but it's having 0 for all the elements.

internal static class FilterService
{
    internal static IEnumerable<TableElement> FilterStructureElements(
      IEnumerable<PdfStructureElement> elements,
      Func<PdfStructureElement, bool> predicate,
    int parentId = 0)
        {
            foreach (var element in elements)
            {
                // If the current element passes the predicate, yield it as a TableElement with the current parentId
                if (predicate(element))
                {
                    yield return element.ToTableElement(parentId);
                    parentId = element.Order;
                }
    
                // Recursively filter children, passing the current parentId to them
                foreach (var child in FilterStructureElements(element.ChildElements, predicate, parentId))
                {
                    yield return child;
                }
            }
        }
    }
  
Was this page helpful?