8

I'm building a WebAPI that is getting its data from legacy systems so no Entity Framework available for me. Now I want to use OData functionality but is not going to work if I have somethink like Entity Framework. But in my research I found out that I can fetch the ODataQueryOptions like this.

    public IQueryable<Vehicle> Get(ODataQueryOptions opts)
    {
        var dal = new DataAccessVehicles();

        return (dal.GetVehicles(opts));                        
    }

In my DAL I could translate the OData query to an actual SQL query. But this does seem like a lot of work.

My question is, is there another way or a better way to achieve this without using Entity Framework. Any tips/help would be appreciated.

  • Why can't you just build an Entity Framework model on top of the existing database? I'm guessing there are a lot of business rules embedded in the legacy libraries, either that or there is no EF provider for the data store..? – Charleh Jul 25 '14 at 12:46
  • @Charleh Not always possible, I had the same problem recently trying to put EF on top of an Oracle database but few of the tables had primary keys so didn't work. – DavidG Jul 25 '14 at 12:48
  • @Charleh There are indeed a lot of business rules inside the legacy code, and I'm not allowed to make any changes to existing code in the libraries. – Maurice van Lieshout Jul 25 '14 at 13:30
  • Not sure, but you might need to implement a custom `IQueryable` implementation – andrew.fox Nov 15 '22 at 13:00

1 Answers1

0

Either enable query support at startup to allow OData queries for all actions, or decorate your actions with [Queryable]. Then make sure your actions return IQueryable, you can use the .AsQueryable() method on your existing collections.

Example:

[Queryable]
public IQueryable<Product> Get() 
{
    return products.AsQueryable();
}

Take a look at this article:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

Troy Carlson
  • 2,965
  • 19
  • 26
  • 1
    I know how to use OData in WebAPI. The products in your example are not all the products I have available, I have to go to the server to get all products and that could be 100000 records. I'm not using Entity Framework. The solution I proposed to my boss is to translate the OData query to an SQL query and return the right dataset. I was only wandering if there is a easier better method. – Maurice van Lieshout Jul 28 '14 at 15:01
  • Gotcha, I misinterpreted your question. As far as I know, my short example using `[Queryable]` and `.AsQueryable()` is not dependent upon Entity Framework, they should work for in-memory collections...but obviously that doesn't help you since you need to avoid putting thousands of objects into an in-memory collection in the first place... – Troy Carlson Jul 28 '14 at 18:43
  • I was looking for something like that as well, but unfortunatelly there isn't any available to quickly implement this, and I don't think MS will release something like this, cause it is in their own interests to have you tied to EF. It requires a bit of work but one way would be to create an action filter and parse the querystring and then build the iqueryable object... – MeTitus Dec 17 '15 at 13:56
  • 1
    http://stackoverflow.com/questions/21461905/how-to-parse-odata-filter-with-regular-expression-in-c – MeTitus Dec 17 '15 at 13:57