Efcore: SelectMany with collection navigation throws

Created on 6 Aug 2019  路  2Comments  路  Source: dotnet/efcore

Tested on SQL Server.

Examples:
```C#
var petNames = context.People.SelectMany(x => x.Pets).ToList();


Unhandled exception. System.InvalidOperationException: SelectMany's collectionSelector was not NavigationExpansionExpression
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessSelectMany(NavigationExpansionExpression source, MethodInfo genericMethod, LambdaExpression collectionSelector, LambdaExpression resultSelector)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryOptimizer.Visit(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorTResult
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQueryTResult
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCoreTResult
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryTResult
at System.Linq.Enumerable.ToListTSource
at Program.Main() in C:StuffThreeOhP7PThreeOhP7PProgram.cs:line 335
at Program.

()


```C#
var petNames = context.People.SelectMany(x => x.Pets.Select(y => y.Name)).ToList();
Unhandled exception. System.ArgumentException: The type or method has 2 generic parameter(s), but 3 generic argument(s) were provided. A generic argument must be provided for each generic parameter.
   at System.RuntimeType.SanityCheckGenericArguments(RuntimeType[] genericArguments, RuntimeType[] genericParamters)
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessSelectMany(NavigationExpansionExpression source, MethodInfo genericMethod, LambdaExpression collectionSelector, LambdaExpression resultSelector)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryOptimizer.Visit(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Program.Main() in C:\Stuff\ThreeOhP7P\ThreeOhP7P\Program.cs:line 335
   at Program.<Main>()



md5-de779045566408fb6322a31814e18c65



Full repo:
```C#
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Pet> Pets { get; set; }
}

public class Pet
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int PersonId { get; set; }
    public Person Person { get; set; }
}

public class BloggingContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");

    public DbSet<Person> People { get; set; }
}

public class Program
{
    public static async Task Main()
    {
        using (var context = new BloggingContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Add(new Person
            {
                Name = "brock",
                Pets = new List<Pet>
                    {
                        new Pet
                        {
                            Name = "max"
                        }
                    }
            });
            context.Add(new Person
            {
                Name = "dom",
                Pets = new List<Pet>
                    {
                        new Pet
                        {
                            Name = "mittens"
                        }
                    }
            });

            context.SaveChanges();
        }

        using (var context = new BloggingContext())
        {
            var petNames = context.People.SelectMany(x => x.Pets.Select(y => y.Name)).ToList();
        }
    }
}
closed-fixed type-bug

Most helpful comment

@smitpatel @maumar This is needed by IdentityServer, so we should get the fix into preview 9

All 2 comments

The overload of SelectMany which does not contain collectionSelector is not processed in navigation expansion yet since we don't translate it.

@smitpatel @maumar This is needed by IdentityServer, so we should get the fix into preview 9

Was this page helpful?
0 / 5 - 0 ratings