问题
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