Search code examples
c#entity-frameworklinqodataexpand

'IQueryable<Entity>' does not contain a definition for 'Expand'


I am trying to expand an entity in OData. I tried Include() but it did not work. Then I am trying Expand() but getting:

'IQueryable<City>' does not contain a definition for 'Expand' and no extension method 'Expand' accepting a first argument of type 'IQueryable<City>' could be found`.

I am doing something like this:

var cities = cityService.All().Expand("State").Expand("State.Country").ToList();

Solution

  • Its a while since I used an OData client, is Expand on DataServiceQuery? In which case you can write and extension method on IQueryable that case.

    public static class IQueryableExtensions
    {
        public static IQueryable<T> Expand<T>(this IQueryable<T> source, string navigationProperty)
        {
            var dsq = (DataServiceQuery<T>)source;
            return dsq.Expand(navigationProperty);
        }
    }
    

    Now you can use it on an IQueryable. If your code is going to call it on IQueryables of other underlying types you will need to handle that behaviour (do nothing, throw error, call Include etc.)

    Its a bit of a hack though, if you are dealing with a DataServiceQuery and you want functionality unique to it you should really be dealing with that type explicitly.

    Sounds like entityService.All() should be returning DataServiceQuery because it matters that its that specific type and not a generic IQueryable.