Andrew Poland
Andrew Poland

Reputation: 185

using odata function isof with web api

I am using OData with web api and attempting to make queries based on type specifics. What I want is to serve a query that looks something like this(where the bolded text isn't syntactically correct but describes what I'm looking to do after asserting the type):

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

however when I try doing this with the [Queryable] attribute and inheriting from ODataController I get a method not supported error with regards to the isof. Is there any way to get the method supported? I have tried adding AllowedFunctions.All to the Queryable attribute but this didn't work.

I am able to use the ODataQueryOptions to check if isof is included in filter and use my own method to check, however it's rather messy and will probably become worse after allowing extra filtering, if a better solution exists that would be awesome!

uncompiled code as it stands currently:

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());

    }
} 

Upvotes: 4

Views: 1667

Answers (2)

Bill
Bill

Reputation: 11

I Do not think the answer match with the question. The original query in question is:

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

I guess it actually means to be:

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

Anyway, it is filter to retrieve entities in a specific derived type or its sub type. The answer does not doing that.

Upvotes: 1

RaghuRam Nadiminti
RaghuRam Nadiminti

Reputation: 6793

We don't support isof and cast functions in web api. We instead support the new cast syntax using separate segments. Your query would look like this with the new syntax,

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

Upvotes: 1

Related Questions