Remove Elements in dgn-file with specific name

I have this:

using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache())
{
LevelHandleCollection levelHandles = levelCache.GetHandles();
foreach (LevelHandle level in levelHandles)
{
if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L"))
{
levelCache.RemoveLevel(null, level);

levelCache.Write();
}
}
}

But it dont remove the levels. I think I need to remove the elements assosiated with the levels before removing the levels. But I dont know how...

Can anyone help?

Parents Reply
  •             using (FileLevelCache levelCache = ActiveDgnFile.GetLevelCache())
                {
                    
                    foreach (DgnModel dgnModel in ActiveDgnFile.GetLoadedModelsCollection())
                    {
                        ModelElementsCollection elementsCollection = dgnModel.GetGraphicElements();
                        LevelHandleCollection levelHandles = levelCache.GetHandles();
    
                        foreach (LevelHandle level in levelHandles)
                        {
                            if (level.Name.EndsWith(" P") || level.Name.EndsWith(" L"))
                            {
                                foreach (Element el in elementsCollection)
                                {
                                    if (level.LevelId == el.LevelId)
                                    {
                                        el.DeleteFromModel();
                                    }
                                }
    
                                levelCache.RemoveLevel(null, level);
                                levelCache.Write();
                            }
    
                        }
                    }
                }

    Now I have this where I compare levelID. I think this might work, what do you think?

Children