views:

219

answers:

3

I'm looking for either a best practise or library for processing string into an object tree..

here is the example:

"[age] = '37' And [gender] Is Not Null And [optindate] > '2003/01/01' And [idnumber] Is Null And ([saresident] = '52' Or [citizenship] Like 'abc%')"

I should be able to objectize this into a tree something like this:

{attribute='age', operator='=', value='37', opperand='And'}
{attribute='gender', operator='Is Not Null', value='', opperand='And'}
{attribute='optindate', operator='>', value='2003/01/01', opperand='And'}

etc....

any suggestions would be great!

+1  A: 

How about the dynamic LINQ library? You could either use "as is", or look at how it builds an Expression<Func<T,bool>> predicate (which is the tree).

Marc Gravell
The problem is that the string is generated by a propriatory filter control. I need to convert the sting to something like an expression tree?
Jan de Jager
+1  A: 

Have a look at Regular Expressions

James
+1  A: 

If you need to store the operations in a tree structure, you should use the postfix or prefix notation. e.g. age = 37 and gender is not null should be stored as

and = age 37 != gender null

so the tree should be like

        and
   =         !=
age 37  gender  null

You can use these links for more details: Notation Used for Operations and Expressions, Conversion and Evaluation with C (All you need to know about Expressions)

Rashmi Pandit
As per my comment above, i don't unfortunately have control over the string that the filtercontrol returns?
Jan de Jager
No, I am not suggesting you change the string. My suggestion is the tree structure should use prefix notation. I.e. 'and' should be root, '=' and '!=' children of 'and' and so on. All operands should be the leaf nodes and children of the respective operators. So when the expression needs to be evaluated, the tree should be parsed accordingly.
Rashmi Pandit
Oh, great will definitely keep this in mind. Thanks!
Jan de Jager
Edited my response to add a couple of links for your reference
Rashmi Pandit