Group & Sort Revit Elements by Parameter

Group & Sort Revit Elements by Parameter

I love LINQ. LINQ, short for Language Integrated Query, allows us to execute list queries in a short and clean manner, rather than using recursive methods. Other than using the Where( ) expression as a filter, the Grouping functions comes in really handy as well.

The code below groups of all Duct elements by its System Classification parameter, i.e. Supply, Exhaust, Return Air.

The groups are essentially Dictionaries, with the System Classification as Key, and its corresponding List of Duct elements as group Elements. Afterwhich, we can simply access its individual elements and its properties by looping through the List.

Document doc = commandData.Application.ActiveUIDocument.Document;
List<Duct> allDucts = new FilteredElementCollector(doc)
var ductGroups =
    from duct in allDucts
    group duct by duct.get_Parameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM).AsString();
foreach (var sysGrp in ductGroups)
    foreach(Duct ductEle in sysGrp)
        //ductEle : Access indivdual duct elements
        //sysGrp.Key : System Classification, i.e. Supply Air, Return Air, Exhaust Air, etc

You can find the source code for the full implementation HERE! Happy coding!

This Post Has 7 Comments

  1. Reynaldo

    Hi, I thought the code was complete, or is it just the idea?

    1. Han

      Hi Reynaldo,

      I’d say its an idea, since what you wish to do with the sorted groups in the foreach loops is entirely up to you.

      Perhaps you could share or DM me what you wish to achieve, and I’ll definitely try my best to help 🙂


  2. Reynaldo

    Hello, my question has nothing to do with the course. Can I shared a shared parameter to a tag by Revit API? I’m not a programmer, that’s why I ask the question, I wrote code, but I can’t associate the labels with the shared parameter. My english.Thanks for your time.(how to associate a shared parameter with a tag Revit API?)

    1. Han

      Hello Reynaldo,

      Apologies, but I can’t picture what you wish to achieve.

      I hope you don’t mind that I’ve dropped an email to you, perhaps you could send me some screenshots to help me understand your problem better.


  3. Reynaldo

    My English is not very good

  4. Carlos Carrillo

    thank han, just i have a question. why you cast to duct if you allready have a list of ducts?

    1. Han

      Hello Carlos,
      its because the FilteredElementCollector defaults to “Element” type, which is the parent class of “Duct”.
      Hence, we have to cast it to “Duct” type to access its class properties & methods.

Leave a Reply