将odata函数isof与webapi结合使用



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

/供应商$filter=isof(产品,namespace.ToyProduct)&产品评级第5页

然而,当我尝试使用[Queryable]属性并从ODataController继承时,我得到了一个不支持的方法错误。有什么方法可以支持该方法吗?我尝试过将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());
    }
} 

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

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

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

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

我想这实际上意味着:

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

无论如何,它是用于检索特定派生类型或其子类型中的实体的过滤器。答案并非如此。

最新更新