How to clone an IPredicate

Posts   
 
    
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 18-Nov-2016 13:08:32   

I have an IPredicate instance passed into a method. I have no idea what is it or where it came from. I need to clone it - how can I do this?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Nov-2016 14:31:43   

Why to clone it, just copy it. It holds no state info or identity to change.

IPredicateExpression filter = new PredicateExpression(CustomerFields.Country == "UK");
filter.AddWithAnd(CustomerFields.City == "London");
var filter2 = new PredicateExpression(filter);

filter2.AddWithOr(CustomerFields.Country == "Germany");
filter2.AddWithOr(CustomerFields.Country == "USA");

filter will hold none of the predicates added to filter2.

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 18-Nov-2016 15:11:42   

I get very confused with this but I think you are referring to IPredicateExpression instances whereas I only have an IPredicate - ie the result of [CustomerFields.Country == "UK"]

My intention after cloning was to invert its Negate property (so it does have state) and then use it elsewhere. So I don't think it would work.

Anyway, I now think I need to put the IPredicate into an IPredicateExpression and Negate that instead. I found GeneralUtils.CreateUsablePredicateExpressionClone() but I will try your new PredicateExpression(filter) since that looks simpler.

Thanks.