There have been a couple of questions regarding the usage of ToElements Method while filtering elements using the FilteredElementCollector Class. It is good to note that the ToElements Method in the FilteredElementCollector Class returns the complete set of elements that meet the specified filter criteria as a generic IList.
However, it's worth noting that some members of the Revit API community tend to use the ToElements Method after using the FilteredElementCollector which in turn increases memory usage and slows down the performance of the same.
One reason for using ToElements is to obtain the element count. However, that can also be achieved by calling GetElementCount.
Another and more valid reason is to access the elements in the list by index, e.g., you have 1000 elements in the list and you want to read their data in a specific order, e.g., #999, #1, #998, #2 or whatever. Then, you need the index provided by the list, and cannot just iterate over them on the predefined order provided by the enumerator.
Here are examples that demonstrate the usage of the two:
//Example 1. //In this example, we use only the FilteredElementCollector to retrieve Wall elements. IEnumerable walls = new FilteredElementCollector(doc).OfClass(typeof(Wall)); foreach (Element item in walls) { ElementId id = item.Id; }
//Example 2. //Here, we use both the FilteredElementCollector and the ToElements Method to collect Wall elements. IList wallList = new FilteredElementCollector(doc).OfClass(typeof(Wall)).ToElements(); foreach (Element item in wallList) { ElementId id = item.Id; }
Here is a sample blog where the same has been discussed.
https://thebuildingcoder.typepad.com/blog/2016/04/how-to-distinguish-redundant-rooms.html#2