In .NET, the FilteredElementCollector class supports the IEnumerable interface for Elements. You can use this class with LINQ queries and operations to process lists of elements. Note that because the ElementFilters and the shortcut methods offered by this class process elements in native code before their managed wrappers are generated, better performance will be obtained by using as many native filters as possible on the collector before attempting to process the results using LINQ queries.
The following example uses an ElementClassFilter to get all FamilyInstance elements in the document, and then uses a LINQ query to narrow down the results to those FamilyInstances with a specific name.
Code Region 6-15: Using LINQ query |
// Use ElementClassFilter to find family instances whose name is 60" x 30" Student ElementClassFilter filter = new ElementClassFilter(typeof(FamilyInstance)); // Apply the filter to the elements in the active document FilteredElementCollector collector = new FilteredElementCollector(document); collector.WherePasses(filter); // Use Linq query to find family instances whose name is 60" x 30" Student var query = from element in collector where element.Name == "60\" x 30\" Student" select element; // Cast found elements to family instances, // this cast to FamilyInstance is safe because ElementClassFilter for FamilyInstance was used List<FamilyInstance> familyInstances = query.Cast<FamilyInstance>().ToList<FamilyInstance>(); |