How to delete all criteria corresponding to a particular field from CriteriaOperator

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 DevExpress.Data.Filtering.Helpers.ClientCriteriaLazyPatcherBase.AggragatesCommonProcessingBase class is used in this example as a base class for the CriteriaPatcherSkipProperties class (in version 17.2 and earlier use the CriteriaPatcherBase class implemented in the example).

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.

protected override CriteriaOperator VisitProperty(OperandProperty theOperand) {
	if(PropertiesToremove.Contains(theOperand.PropertyName)) return null;
	return theOperand;
}
Protected Overrides Function VisitProperty(ByVal theOperand As OperandProperty) As CriteriaOperator
	If PropertiesToremove.Contains(theOperand.PropertyName) Then
		Return Nothing
	End If
	Return theOperand
End Function

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

Description

Replaced CriteriaPatcherBase with ClientCriteriaLazyPatcherBase.


Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)