Create an ODataQueryOptions programmatically

为君一笑 提交于 2019-12-25 03:17:08

问题


My API currently has some OData endpoints that use generic ODataQueryOptions to recieve an OData query...

[HttpGet]
[Route("search")]
public object Search(ODataQueryOptions<MySearchableEntity> options)
{
    return searchService.Search(options);
}

searchService then traverses the Expression of options.Filter.FilterClause to build a custom SQL query. It does similar stuff for the order, top and skip parameters.

This is all working fine, but I now need it to parse a query for a dynamic type.

Is it possible to programmatically build an EdmModel and IEdmType etc for the ODataQueryContext, without having a 'real' CLR type backing it up?

The 'type' is well defined in that we have classes describing its shape, but it doesn't exist as a CLR type.


回答1:


Answering my own question, as I've found the answer.

The ODataQueryContext that is used to validate the query accepts a Type, but there is an overload that accepts an IEdmType, which in turn can be an EdmModel, which is very easy to build in a bunch of different ways.

Docs here:

http://odata.github.io/WebApi/02-01-model-builder-abstract/

http://odata.github.io/WebApi/02-02-model-builder-untyped/

http://odata.github.io/WebApi/02-03-model-builder-nonconvention/

http://odata.github.io/WebApi/02-04-convention-model-builder/



来源:https://stackoverflow.com/questions/52663683/create-an-odataqueryoptions-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!