3

I have an ASP.Net WebAPI service that accepts OData queries. One of my controller methods accepts an ODataQueryOptions instance and applies it to an IQueryable instance. So far so good. Everything is working as planned.

Now, I have a specific need to obtain one of the key/value pairs from the OData query. For example, if I have two filters specified (someproperty eq 123 and otherproperty eq 456), how can I obtain one of the key/value pairs from the raw filter? I've looked at some of the ODataUriParser documentation, but it is really complicated and seems overkill. Isn't there an easier way to obtain simple key/value pairs, like I would from a normal QueryString?

EDIT: I've since put in some manual string parsing to accomplish what I needed, but that's obviously not ideal. Therefore, I'm putting up a bounty on this question. Hopefully someone has a simple solution to this!

Kilhoffer
  • 32,375
  • 22
  • 97
  • 124
  • 1
    Can you show what source code you have already (maybe with some mocked up data and options/filters)? – peaceoutside Nov 05 '14 at 22:18
  • 1
    Take a look at [This](http://stackoverflow.com/questions/21461905/how-to-parse-odata-filter-with-regular-expression-in-c) Regex based solution. – gunvant.k Nov 05 '14 at 23:48
  • How you want to access to this filters? You are talking about `like I would from a normal QueryString` this is not query string because it has and/or/not operators. I suppose the ideal is parsing to expression tree, but framework does this for you. I have an example implementing of `IQueryable` and passing it to OData. – Pavel Nov 06 '14 at 10:36
  • @Pavel: I already have such an implementation and use it everywhere else, but with this one query, I need to know if there exists a way for me to extrapolate key value pairs from the raw odata query itself. For this purpose, I dont even care about the and/or/not operators. – Kilhoffer Nov 06 '14 at 21:39
  • @Glk.net: This is perfect! It still requires manual parsing of the raw query, but it does so quite well. I can just wrap that in some C# code to give it a more objectified structure and I'm done. THANK YOU! Can you post something with the usage as an answer so I can reward you the bounty? – Kilhoffer Nov 06 '14 at 21:42

1 Answers1

1

There is an easy approach using regex where you convert your Odata query filter into key Value pair , regex found from this answer Here

string strRegex = @"(?<Filter>" + 
"\n" + @"     (?<Resource>.+?)\s+" + 
"\n" + @"     (?<Operator>eq|ne|gt|ge|lt|le|add|sub|mul|div|mod)\s+" + 
"\n" + @"     '?(?<Value>.+?)'?" + 
"\n" + @")" + 
"\n" + @"(?:" + 
"\n" + @"    \s*$" + 
"\n" + @"   |\s+(?:or|and|not)\s+" + 
"\n" + @")" + 
"\n";

your replacement string is something like,

string strReplace = @"${Resource}:${Value}," 

Which gives you output like,

someproperty:123,otherproperty:456

Then convert that string into dictionary of your KeyValue parameter or however you want to consume it

public Dictionary<String, String> getKeyValue(String input)
    {

           return input.Split(',').ToDictionary(kv => kv.Split(':').First(), kv => kv.Split(':').Last());

    }
Community
  • 1
  • 1
gunvant.k
  • 1,096
  • 1
  • 10
  • 15