When testing for equality, compare names or ids, not the elements

I wrote this code to find which level types are used and not used.

public void levelTypes()
    Document doc = this.ActiveUIDocument.Document;
    string usedLevelTypes = "";
    string notUsedLevelTypes = "";
    foreach (LevelType levelType in new FilteredElementCollector(doc).OfClass(typeof(LevelType)).Cast<LevelType>())
        if (new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>().Where(level => level.LevelType == levelType).Count() > 0)
            usedLevelTypes += levelType.Name + ", ";
            notUsedLevelTypes += levelType.Name + ", ";
    TaskDialog.Show("Level Types",
                    "Used\n----\n" + usedLevelTypes + 
                    "\n\nNot Used\n--------\n" + notUsedLevelTypes);

But it doesn’t work, because this always is false:

level.LevelType == levelType

Instead, do this

level.LevelType.Name == levelType.Name

Or do this

level.LevelType.Id == levelType.Id

You will be much happier and much less frustrated. I promise.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s