private CriteriaOperator Extract(CriteriaOperator criteriaOperator, string matchString, CriteriaOperator replaceOperator) { if (criteriaOperator is BinaryOperator) { var binaryOperator = (BinaryOperator)criteriaOperator; binaryOperators.Add(binaryOperator); return(binaryOperator); } if (criteriaOperator is NullOperator) { nullOperators.Add((NullOperator)criteriaOperator); return(criteriaOperator); } if (criteriaOperator is NotOperator) { var notOperator = (NotOperator)criteriaOperator; notOperators.Add(notOperator); Extract(notOperator.Operand); } else if (criteriaOperator is UnaryOperator) { unaryOperators.Add((UnaryOperator)criteriaOperator); return(criteriaOperator); } else if (criteriaOperator is GroupOperator) { var groupOperator = (GroupOperator)criteriaOperator; CriteriaOperatorCollection operands = groupOperator.Operands; var indexesToremove = new List <int>(); for (int i = 0; i < operands.Count; i++) { CriteriaOperator operand = operands[i]; if (operand.ToString() == matchString) { if (ReferenceEquals(replaceOperator, null)) { indexesToremove.Add(i); } else { operands[i] = replaceOperator; } } else { CriteriaOperator extract = Extract(operand); operands.RemoveAt(i); operands.Insert(i, extract); } } foreach (int i in indexesToremove) { operands.RemoveAt(i); } } else if (criteriaOperator is ContainsOperator) { var containsOperator = (ContainsOperator)criteriaOperator; CriteriaOperator condition = containsOperator.Condition; Extract(condition); } return(criteriaOperator); }