2

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
  • I have to mention that the expression will be used to query data in an EAV schema (Please no comments)? Thus i need to build this into an ExpressionTree (or something) to then recurse and build the necessary SQL queries. Commented Jun 19, 2009 at 7:37

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

As per my comment above, i don't unfortunately have control over the string that the filtercontrol returns?
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.
Edited my response to add a couple of links for your reference
1

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).

1 Comment

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?
1

Have a look at Regular Expressions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.