Unhandled exception System.NullReferenceException while trying to query Imported Function with empty navigation property inside of result set RRS feed

  • Question

  • Hi!

    I'm getting exception when trying to query function GetFriendsEatings and in the result there is entry Eating without related Dish and Category -

    Function works fine if all Dishes and Categories in place for every entry of result set.

    The same query for entity Eatings works well in any cases - /Eatings?$expand=Dish/Categories

    Why the behavior is different and how I can workaround this?

    public IQueryable<Eating> GetFriendsEatings(string friend)
      return this.CurrentDataSource.GetFriendsEatings(friend).AsQueryable<Eating>();

    <stacktrace>at lambda_method(Closure , Eating ) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Data.Services.Providers.BasicExpandProvider.ExpandedEnumerator`1.MoveNext() at System.Data.Services.DataService`1.SerializeResponseBody(RequestDescription description, IDataService dataService) at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description) at System.Data.Services.DataService`1.HandleRequest()</stacktrace>


    Saturday, January 15, 2011 5:14 PM


  • Hi,

    I assume that you have a data service based on the EF provider and the above service operation which creates a List<T> and then calls AsQueryable on it. This is unfortunately currently not supported. The reason is that the service is using two different LINQ providers. EF has it's own LINQ to EF and then in your service operation you're using LINQ to Objects (List<T>.AsQueryable). These two providers require a little bit different shapes of the expressions to process certain operations. The DataService will generate the expression trees appropritate for the provider of the main service, in your case the EF provider. It will use the same approach even for service operations. The reason for the failure is very probably the fact that in EF the expression tree must not include so called null propagation (conditional expressions dealing with possible null values), since the EF uses DB semantics which automatically handles these nulls. LINQ to Objects on the other hand requires the null propagation in the expression (just like C# does), otherwise one might get NullReferenceException. This is very likely the case of your error.

    You might change your service operation to return an EF based queryable, if that's possible, in which case it should work. If that's not possible the only other solution is to wrap the LINQ to Objects queryable returned from the service operation and inject the null propagation manually. This is rather complicated and very hard to make work correctly in all cases.

    The other possibility is to not use expansion or navigation over navigation properties which might have null values, as the problem will only occur when null values are found (especially null values of navigation properties).


    Vitek Karas [MSFT]
    Saturday, January 15, 2011 6:03 PM