Building predicates (filters) dynamically

Posts   
 
    
Posts: 94
Joined: 23-Aug-2006
# Posted on: 25-Apr-2013 01:56:21   

I'm not sure if this is even possible ... but here it goes...

I have a set of key value pairs that correspond to Entitityfield name and value - field meaning the Field Creation Class. For example CustomerFields.FirstName or CustomerFields.LastName

My key values pairs look similar to FirstName Frans LastName Bouma

Is there a way that I can build a PredicateExpression dynamically from these ?

In one case I may get the above example data, in another case it would be a different set of Fields and Values that need to be put together in Predicates on the fly in order to produce a different where clause for a QuerySpec query.

Any advise is appreciated.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Apr-2013 07:58:58   

If I understood right, you have a Dictionary<EntityFieldCore, object> with (Field,Value) pairs. If so, why not do something like this?

var filter = new PredicateExpression();
foreach(var pair in myPredicateDicionary)
{
     filter.Add(pair.Key == pair.Value);
}
// use filter

Does that work? or Did I miss something?

David Elizondo | LLBLGen Support Team
Posts: 94
Joined: 23-Aug-2006
# Posted on: 25-Apr-2013 15:23:44   

Hi David - I have a string array coming into the method. It starts with a user selecting a couple of columns in a javascript grid and setting a filter value on each column. By the time the code reaches LLBLGen it the equivalent an array of key/value pairs ( all string based)

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 25-Apr-2013 23:20:55   

You can get hold on the entityField by the following:

EntityField2 field = entity.Fields["FIELD_NAME"];
Posts: 94
Joined: 23-Aug-2006
# Posted on: 26-Apr-2013 01:56:49   

Thanks! I will try that. It certainly looks like that should totally work.