How would I go about getting a collection of levels sorted by elevation? From reading the info I've found so far I think that filtered element collector returns an Ienumerable and that can be sorted by using "OrderBy", then ToElementIds could be used to create the collection of element ids in the correct order. Is that correct? Below is some code I tried and didn't work. Can someone please point me in the right direction?
Also, is an Icollection sortable?
UIDocument uiDoc = this.ActiveUIDocument; Document doc = uiDoc.Document; FilteredElementCollector levCollector = new FilteredElementCollector(doc);
ICollection<Element> levelsCollection = levCollector.OfClass(typeof(Level)).OrderBy(lev => lev.Elevation).ToElementIds();
Solved! Go to Solution.
Solved by Aaron.Lu. Go to Solution.
public static IOrderedEnumerable<Level> FindAndSortLevels(Document doc)
{
return new FilteredElementCollector(doc)
.WherePasses(new ElementClassFilter(typeof(Level), false))
.Cast<Level>()
.OrderBy(e => e.Elevation);
}
You can change
ICollection<Element> levelsCollection = levCollector.OfClass(typeof(Level)).OrderBy(lev => lev.Elevation).ToElementIds();
to this:
List<Level> levelsCollection = levCollector.OfClass(typeof(Level)).OfType<Level>().OrderBy(lev => lev.Elevation).ToList();
1. Return value can't be ICollection<Element> when you call .ToElementIds
2. OfClass() method will return a collection of Element, you can only order them by elevation after casting Element to Level, that's why I suggest using OfType<Level>()
3. OrderBy() method returns IOrderedEnumerable<T>, if you want to have index operator, i.e. list[i], you can call ToList() to let it return a List<T>.
Hey guys, thank you for all your cool suggestions!
I summarised the discussion and added my contribution to it on The Building Coder:
http://thebuildingcoder.typepad.com/blog/2014/11/webgl-goes-mobile-and-sorted-level-retrieval.html#3
I also added my code snippet suggestion to The Building Coder samples:
https://github.com/jeremytammik/the_building_coder_samples
Cheers,
Jeremy