I would also like to reuse an IPredicateExpression object, but have encountered these issues:
1) Invoking the Clear method of the IPredicateExpression object appears to have no effect as all my original parameters remain intact.
2) Setting the IPredicateExpression object to a new instance clears the parameters, but I'm unable to add new filter expression parameters. I do not receive an exception.
Code:
IPredicateExpression someFilter= new PredicateExpression();
someFilter.Add(EntityFields.SomeField== someValue);
List<int> entityIndex= new List<int>();
// retrieve the collection using the filter
EntityCollection.GetMulti(someFilter);
//now filter the collection within a loop based on another entity collection
foreach (EntityClass childEntity in parent.childEntityCollection)
{
someFilter= new PredicateExpression(); //parms cleared, but can't add new ones
someFilter.Clear(); //parms not cleared
someFilter.Add(EntityFields.SomeField== childEntity.SomeCustomProperty);
//should only have a single match here
entityIndex= EntityCollection.FindMatches(someFilter);
// get a reference to entity we just found
Entity= EntityCollection[entityIndex[0]];
}