- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to get a list of ElementId of FabricationParts filtered by their shared parameter. I thought using LINQ statement should be faster than using a while-loop. But the result is the opposite. Both are slow; but my LINQ statement is slower than my while-loop. I try to find a way to speed up my LINQ.
In the examples below, I try to use LINQ to retrieve a list of ElementId of FabricationPart whose shared parameter "OurMechNum" is "123". This LINQ takes 13 seconds to finish:
List<ElementId> lisFoundPcElmIds =
new FilteredElementCollector( doc )
.OfCategory( BuiltInCategory.OST_FabricationDuctwork )
.WhereElementIsNotElementType()
.Where( x => x.LookupParameter( "OurMechNum" ) != null &&
x.LookupParameter( "OurMechNum" ).HasValue &&
x.LookupParameter( "OurMechNum" ).AsString().Equals( "123" ) )
.Select( x => x.Id )
.ToList();
On the other hand, using a while-loop to do the same thing (like the one shown below) takes 7 seconds:
FilteredElementCollector collDuct =
new FilteredElementCollector( doc )
.collDuct.OfCategory( BuiltInCategory.OST_FabricationDuctwork )
.WhereElementIsNotElementType();
List<ElementId> lisFoundPcElmIds = new List<ElementId>();
FilteredElementIterator eit = collDuct.GetElementIterator();
eit.Reset();
while( eit.MoveNext() )
{
FabricationPart curPc = eit.Current as FabricationPart;
if ( curPc != null )
{
// Filter out the current piece if it doesn't have mechanical number 123.
Parameter oParamMchNum = curPc.LookupParameter( "OurMechNum" );
if ( oParamMchNum == null )
continue;
if ( ! oParamMchNum.HasValue )
continue;
if ( ! oParamMchNum.AsString().Equals( "123" ) )
continue;
// Here, we have found a piece that has mechanical number 123.
lisFoundPcElmIds.Add( curPc.Id );
}
}
Both versions are slow. But somehow my LINQ is slower than my while-loop. This is odd.
Seem like the reason why the LINQ is slow has to do with the filter of the shared parameter. If I remove the filter of the shared parameter "OurMechNum" like the LINQ statement shown below, the LINQ statement runs very fast (like 1 second):
List<ElementId> lisFoundPcElmIds =
new FilteredElementCollector( doc )
.OfCategory( BuiltInCategory.OST_FabricationDuctwork )
.WhereElementIsNotElementType()
.Select( x => x.Id )
.ToList();
Unfortunately, I cannot remove the filter for my application. What should I do to improve the query?
Please help. Thanks,
JC_BL
Solved! Go to Solution.