Peter Goodman bio photo

Peter Goodman

A software engineer and leader living in Auckland building products and teams. Originally from Derry, Ireland.

Twitter Google+ LinkedIn Github

Following on from my previous post on creating a client API for ASP.Net Web API queryable oData services, I wanted to prove that expand clauses could also work for Entity Framework object sets.

The oData URI conventions specifies that the desired depth of an entity can be represented in the $expand query string parameter. When using the ASP.Net Web API we need a way to pull this information from the querystring and convert it into Include statements on the ADO.Net Entity Framework object set. To achieve this I created an extension method that will pull the $expand query string parameter and translate it into multiple Include method calls.

public class NorthwindController : ApiController
{
    private readonly NorthwindEntities northwindEntities = new NorthwindEntities();

    public NorthwindController() {
        northwindEntities.ContextOptions.LazyLoadingEnabled = false;
    }

    public IQueryable<Customer> GetCustomers() {
        return northwindEntities.Customers.ProcessExpands();
    }   
}

public static class ObjectQueryExtensions {
    public static ObjectQuery<T> ProcessExpands<T>(this ObjectSet<T> source) where T : class {
        string expandsQueryString = HttpContext.Current.Request.QueryString["$expand"];
        if(string.IsNullOrWhiteSpace(expandsQueryString)) {
            return source;
        }

        ObjectQuery<T> query = source;

        expandsQueryString.Split(',').Select(s => s.Trim()).ToList().ForEach(
            expand => {
                query = query.Include(expand.Replace("/", "."));
            });

        return query;
    }
}

Now all that’s left is to figure out how to provide the client side support for this….

Meanwhile the full source for the above project is available on github. https://github.com/PeteGoo/WebApiContext.Sample