This example demonstrates how to implement a helper that removes all expressions referencing a certain property from the CriteriaOperator. For example, you may want to programmatically remove fragments like "[SavedPassword] = '***'" from filter expressions that persist on client computers, because you decided to remove the SavedPassword property from the model for security reasons.
Although this task can be accomplished using regular expressions, this approach is too complicated and is applicable only to CriteriaOperator expressions that follow a certain pattern. This example uses a different technique, which allows you to support expressions of any complexity with minimum effort.
The solution demonstrated in this example uses the concept described in this Knowledge Base article: Implementation of the base class for a CriteriaOperator expression patcher. The CriteriaPatcherBase class is used in this example as a base class for the CriteriaPatcherSkipProperties class.
To remove any reference to a specific property from the CriteriaOperator expression, override the CriteriaPatcherBase.VisitProperty method, and return null (Nothing in Visual Basic) if the property name matches the name of a property that should be removed.
[C#]protectedoverrideCriteriaOperatorVisitProperty(OperandPropertytheOperand){if(PropertiesToremove.Contains(theOperand.PropertyName))returnnull;returntheOperand;}
[VB.NET]ProtectedOverridesFunction VisitProperty(ByVal theOperand As OperandProperty) As CriteriaOperatorIf PropertiesToremove.Contains(theOperand.PropertyName) ThenReturnNothingEndIfReturn theOperandEndFunction
However, this is incomplete. The expression will contain invalid statements if you simply remove properties from it: "() = #2015-12-30# And StartsWith([City], 'q')". To delete invalid statements from the expression, override the VisitAggregate, VisitFunction, VisitGroup, VisitIn, VisitUnary, VisitBinary, and VisitBetween methods. The implementation of overridden methods is demonstrated in the CriteriaPatcherSkipProperties.cs and CriteriaPatcherSkipProperties.vb files.
See also:
Implementation of the base class for a CriteriaOperator expression patcher
How to create a custom converter to convert the CriteriaOperator to the System.String type