Search code examples
c#.netodata

Odata - how to parse filters


I have Odata filter which is string and looks like that:

string odataFilter = "name eq 20 and surname eq 50"

What I need to accomplish is to get following dictionary from it:

dict["name"] = "20"
dict["surname"] = "50"

What is also important: I want to handle only "and" and "eq" keywords from odata. Nothing else.

Does anyone have an idea how to touch the problem? Maybe by regular expressions?


Solution

  • After few hours I created a regex which fits my needs very well. It accepts text values only as a string between apostrophes ('). In addition you can pass number value without them.

    Example: name eq 'John' and age eq 75

    Results in regex are grouped so it easily to get them from matches collection.

    "(?<PropertyName>\w+?)\s+(?<Operator>eq)\s+(?<Value>(['])(\\?.)*?\1|\d+(\.\d+)?(\s*|$))(?:\s*$|\s+(?:and)(\s+|$))"