.NET では、FilteredElementCollector クラスが Elements の IEnumerable インタフェースをサポートします。要素のリストを処理するには、このクラスとともに LINQ クエリーと操作を使用します。このクラスが提供する ElementFilters とショートカット メソッドは、管理されたラッパーが作成される前に、要素をネイティブ コードで処理するため、作成された LINQ クエリーを使用して処理を試みる前に、コレクタにできるだけ多くのネイティブ フィルタを使用することでパフォーマンスを向上させることができます。
次の例では、ドキュメント内のすべての FamilyInstance 要素を取得するために ElementClassFilter を使用し、特定の名前でこれらの FamilyInstances に結果を絞り込むために LINQ クエリーを使用します。
コード領域 6-15: LINQ クエリーを使用 |
// 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>(); |