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.
我有一个ASP。接受OData查询的WebAPI服务。我的一个控制器方法接受ODataQueryOptions实例并将其应用到IQueryable实例。目前为止一切都很顺利。一切都按计划进行。
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?
现在,我需要从OData查询中获得一个键/值对。例如,如果我指定了两个过滤器(someproperty eq 123和otherproperty eq 456),如何从原始过滤器获得一个键/值对?我已经看过一些ODataUriParser文档,但是它真的很复杂,而且看起来有些过头了。难道没有一种更简单的方法来获取简单的键/值对吗?
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!
编辑:我已经进行了一些手动字符串解析,以完成我所需要的,但这显然不是理想的。因此,我悬赏要问这个问题。希望有人有一个简单的解决办法!
1 个解决方案
#1
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
使用regex有一种简单的方法,您可以将Odata查询过滤器转换为键值对,regex在这里从这个答案中找到
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
然后将该字符串转换为KeyValue参数的字典或您希望使用它的方式
public Dictionary<String, String> getKeyValue(String input)
{
return input.Split(',').ToDictionary(kv => kv.Split(':').First(), kv => kv.Split(':').Last());
}
#1
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
使用regex有一种简单的方法,您可以将Odata查询过滤器转换为键值对,regex在这里从这个答案中找到
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
然后将该字符串转换为KeyValue参数的字典或您希望使用它的方式
public Dictionary<String, String> getKeyValue(String input)
{
return input.Split(',').ToDictionary(kv => kv.Split(':').First(), kv => kv.Split(':').Last());
}