7
votes

Consider the following method in a Web Api controller:

[Queryable(AllowedQueryOptions= AllowedQueryOptions.All)]
public override IQueryable<Mandate> Get()
{
        return new List<Mandate>() { new Mandate() { 
            Id = 1, 
            PolicyNumber = "350000000",
            OpenPositions = new List<OpenPosition>(){ 
                new OpenPosition(){ Id = 1, Amount =2300 },
                new OpenPosition(){ Id = 2, Amount =2100 }
            }},
            new Mandate() { 
                Id = 2, 
                PolicyNumber = "240000000" ,
                OpenPositions = new List<OpenPosition>(){ 
                new OpenPosition(){ Id = 3, Amount =2500 },
                new OpenPosition(){ Id = 2, Amount =2100 }
            }

            } }.AsQueryable<Mandate>();
    }

Here the list is built manually and if I browse to the following url:

http://localhost:52446/odata/Mandates?$filter=Id eq 1 it returns the correct item from the list.

Now obviously the list is more likely to be a database structure. Data would be retrieved using some ORM and returned to the Web API service.

I don't use Entity Framework (and I can't because of legacy systems).

How would I use Web API in this case? How would I translate the url parameters so that the filters are applied by the layer responsible of the data access?

1
If there is no LINQ provider for your database, you would need to translate the ODataQueryOptions. Start with this blog post: blogs.msdn.com/b/webdev/archive/2013/02/25/… - Mike Wasson
Got it. You pointed me into the right direction with your LINQ provider. I found out I can do it easily with the ORM we are using (OpenAccess). More info here : documentation.telerik.com/openaccess-orm/developers-guide/… - Sam
@Sam could you post your comment as answer? Otherwise the questions seems like active even though it is resolved. - Sebastian K
sure, I'll do that now. - Sam

1 Answers

0
votes

Got it. You pointed me into the right direction with your LINQ provider. I found out I can do it easily with the ORM we are using (OpenAccess). More info here :http://docs.telerik.com/data-access/developers-guide/using-web-services/asp.net-web-api/developer-guide-wcfservices-web-api-expose-oacontext