efcore icon indicating copy to clipboard operation
efcore copied to clipboard

GroupBy throws NRE when grouping using navigation properties

Open dealdiane opened this issue 11 years ago • 2 comments

Suppose we have the following:

public class Handler
{
    public int Id { get; set; }

    public string Name { get; set; }

    public Pet Pet { get; set; }

    public int PetId { get; set; }
}

public class Pet
{
    public int Id { get; set; }

    public string Name { get; set; }

    public PetType PetType { get; set; }
}

public enum PetType
{
    Dog,
    Cat
}

using this query fails with an NRE:

db.Set<Handler>()
    .GroupBy(p => p.Pet.PetType)
    .ToList();

Stack trace:

NullReferenceException: Object reference not set to an instance of an object.
    Microsoft.Data.Entity.Query.QueryResultScope._GetResult[TResult](IQuerySource querySource)
    lambda_method(Closure , QueryResultScope )
    System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
    System.Linq.GroupedEnumerable`3.GetEnumerator()
    System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
    Microsoft.Data.Entity.Query.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
    System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

This issue could be related to this although I don't see the use of navigation properties in that.

dealdiane avatar Nov 01 '15 20:11 dealdiane

Attempted to reproduce this issue with EntityFramework.SqlServer 7.0.0-rc1-final but instead of a NullReferenceException an InvalidOperationException ("Sequence contains more than one element") was thrown. Testing against current dev, the same query did not throw any exception and produced the correct output.

mikary avatar Apr 21 '16 18:04 mikary

Appears to be resolved by other changes, added tests in #5141.

mikary avatar Apr 21 '16 20:04 mikary