Turning a flat nested set into a hierarchy
What about idea (and removing categoryDtos.RemoveAll(x =&gt; x.Depth &gt; 0); // Terrible):

if (parent != null)
{
    filteredCategories = categories.Where(x =&gt; x.Depth == (parent.Depth + 1) &amp;&amp;
                                               x.Left &gt; parent.Left &amp;&amp;
                                               x.Right &lt; parent.Right).ToList();
}
else
{
    filteredCategories = categories.Where(x =&gt; x.Depth == 0).ToList();
}


Or, if the function is used somwhere else as well, maybe additional parameter to function like bool onlyTopLevel.