Node element contains next fields
Id - node id
ParentId - id of parent node
HierarchyPoint - path of node

Optimize next LINQ query:


var relatedEntities = await dbContext.NaumenEntities
    .Where(ne => ne.ParentId == parentEntity.Id &&
        (ne.OperationName == OperationName.SetOgranizationUnit ||
            ne.OperationName == OperationName.SetEmployee)).ToListAsync(cancellationToken);
foreach (var entity in relatedEntities)
{
    var subLevels = await dbContext.NaumenEntities
        .Where(ne => ne.ParentId != parentEntity.Id &&
                        ne.HierarchyPoint.StartsWith(entity.HierarchyPoint) &&
                        (ne.OperationName == OperationName.SetOgranizationUnit ||
                        ne.OperationName == OperationName.SetEmployee)).ToListAsync(cancellationToken);
    foreach (var leaf in subLevels)
    {
        leaf.HierarchyPoint = leaf.HierarchyPoint.Replace("?/", parentEntity.HierarchyPoint);
        if (!leaf.HierarchyPoint.StartsWith("?/"))
        {
            leaf.Status = Status.Ready;
        }
    }

    entity.HierarchyPoint = entity.HierarchyPoint.Replace("?/", parentEntity.HierarchyPoint);
    if (!entity.HierarchyPoint.StartsWith("?/"))
    {
        entity.Status = Status.Ready;
    }
}
