以编程方式创建 ODataQueryOptions

Create an ODataQueryOptions programmatically

我的 API 目前有一些使用通用 ODataQueryOptions 接收 OData 查询的 OData 端点...

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

searchService 然后遍历 options.Filter.FilterClauseExpression 以构建自定义 SQL 查询。它对 order、top 和 skip 参数做类似的事情。

一切正常,但我现在需要它来解析动态类型的查询。

是否可以在没有 'real' CLR 类型支持的情况下以编程方式为 ODataQueryContext 构建 EdmModel 和 IEdmType 等?

'type' 定义明确,因为我们有 类 描述其形状,但它不作为 CLR 类型存在。

回答我自己的问题,因为我找到了答案。

用于验证查询的 ODataQueryContext 接受 Type,但有一个重载接受 IEdmType,而 IEdmType 又可以是 EdmModel,即很容易以多种不同的方式构建。

此处的文档:

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/