Search code examples
linqentity-frameworkodata

How do you expose a Web API OData service that returns a combination of Entity Framework and File System results?


For example, I have a database table that contains the following fields:

ID 
Type
FilePath

An Entity Framework model is created for that table. The model is used to create an EDM for an OData service.

I would like to use that Odata service to filter the database fields as well as filter the results based on the content of the files located at the file path.

For example I'd like to be able to use syntax like this to get all records where the Type starts with the word 'procedure' and the content contains the word 'test':

$filter=startswith(Type,'procedure') and substringof('test', FileContent)

I would like the database filters to be applied first so that there are less FileContents to read. I would also like to read the FileContents one by one using a stream instead of loading them all into memory.

Can this be accomplished through LINQ by creating some sort of IQueryable object? Can this be accomplished through Web API OData by attaching a property to the EDM and treating it differently?


Solution

  • It may be resolved in this way:

    1. Add a property, say Content to the entity type.
    2. Derive ODataQueryOptions and overwrite its ApplyTo method. This is the base class: https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http.OData/OData/Query/ODataQueryOptions.cs

    3. Use the derived class in the controller:

      public XXXXXController
      {
          public IHttpActionResult(MyODataQueryOptions options)
          {
              var products = this.GetProducts().AsQueryable();
      
              return Ok(options.ApplyTo(products));
          }
      }