【问题标题】:using odata function isof with web api使用带有 web api 的 odata 函数 isof
【发布时间】:2013-06-23 00:31:24
【问题描述】:

我将 OData 与 Web api 一起使用,并尝试根据类型细节进行查询。我想要的是提供一个看起来像这样的查询(粗体文本在语法上不正确,但描述了我在断言类型后要做什么):

../Supplier?$filter=isof(Product, namespace.ToyProduct)&Product.ratedAge eq 5

但是,当我尝试使用 [Queryable] 属性并从 ODataController 继承时,我得到一个关于 isof 的 method not supported error。有什么方法可以支持该方法吗?我尝试将 AllowedFunctions.All 添加到 Queryable 属性,但这没有用。

我可以使用 ODataQueryOptions 检查过滤器中是否包含 isof 并使用我自己的方法进行检查,但是它相当混乱,并且在允许额外过滤后可能会变得更糟,如果存在更好的解决方案那就太棒了!

当前未编译的代码:

public class SupplierController : ODataController {
    [Queryable(AllowedFunctions = AllowedFunctions.All)]
    public PageResult<Supplier> Get(ODataQueryOptions<Supplier> queryOptions) {

        // doesn't work, method not supported
        //var queryableSuppliers = queryOptions.ApplyTo(_repository.All());  

        // works but messy method.
        var queryableSuppliers = MyIsOfMethod(queryOptions.Filter) 

        return new PageResult<ObjectType>(
            results as IEnumerable<ObjectType>,
            Request.GetNextPageLink(),
            Request.GetInlineCount());

    }
} 

【问题讨论】:

标签: .net asp.net-web-api odata


【解决方案1】:

我认为答案与问题不匹配。 有问题的原始查询是:

../Supplier?$filter=isof(Product, namespace.ToyProduct)&Product.ratedAge eq 5 

我猜它实际上意味着:

../Supplier?$filter=isof(Product, namespace.ToyProduct)%20and%20Product.ratedAge eq 5 

无论如何,它是检索特定派生类型或其子类型中的实体的过滤器。 答案没有这样做。

【讨论】:

    【解决方案2】:

    我们不支持 web api 中的 isof 和 cast 函数。相反,我们使用单独的段支持新的强制转换语法。使用新语法,您的查询将如下所示,

    ../Supplier?$filter=Product/namespace.ToyProduct/ratedAge eq 5

    【讨论】:

    • 您能否提供有关此新语法以及如何使用它的文档的链接?我遇到了与 OP 相同的问题,但我无法让您的建议发挥作用。我正在使用isof 函数的另一个“重载”,它只采用单引号中的完全限定类型名称。还有,当初不支持的原因是什么?
    猜你喜欢
    • 1970-01-01
    • 2015-01-03
    • 2014-06-11
    • 2014-10-08
    • 2013-08-24
    • 1970-01-01
    • 2016-09-13
    • 2013-01-12
    • 2012-02-02
    相关资源
    最近更新 更多