Repository navigation
Milestone
Description
Tested on SQL Server.
Examples:
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.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>()
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>()
Workaround: evaluate the SelectMany on the client:
var petNames = context.People
.Include(e => e.Pets)
.AsEnumerable()
.SelectMany(x => x.Pets.Select(y => y.Name)).ToList();Full repo:
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();
}
}
}
Activity
smitpatel commentedon Aug 6, 2019
The overload of SelectMany which does not contain collectionSelector is not processed in navigation expansion yet since we don't translate it.
ajcvickers commentedon Aug 6, 2019
@smitpatel @maumar This is needed by IdentityServer, so we should get the fix into preview 9
Relational: Translate SelectMany
Relational: Translate SelectMany
Relational: Translate SelectMany
Relational: Translate SelectMany