© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
9 replies
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;
                }
            }
        }
    }
  
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;
                }
            }
        }
    }
  
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ sending files through a discord webhook
C#CC# / help
3y ago
Memory Referrence Issue In Recursive Algorithm
C#CC# / help
3y ago
C# redirect after sending file through headers
C#CC# / help
3y ago
❔ Recursive Multiplier
C#CC# / help
3y ago