dotnet / dotnet/efcore

SelectMany from SelectMany with sub-Select fails to translate

Open
#37,912 1 comment 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### Bug description

SelectMany from SelectMany with sub-Select fails to translate.
See example code, the "var fail" sql fails to translate. This happens because of sub-select ".Select(y => new { y })" used within first SelectMany, event though the result should be exactly same as "ok" case.

### Your code

```csharp
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace ConsoleApp1;

static class Program
{
static void Main()
{
var c = new Context();

var ok = c.Set()
.SelectMany(x => x.E2S)
.SelectMany(x => x.E3S)
.ToQueryString();

var fail = c.Set()
.SelectMany(x => x.E2S.Select(y => new { y }))
.SelectMany(x => x.y.E3S)
.ToQueryString();
}
}

class Context : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);

optionsBuilder.UseSqlite("Data Source=:memory:;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity();
modelBuilder.Entity().HasOne(x => x.E1).WithMany(x => x.E2S);
modelBuilder.Entity().HasOne(x => x.E2).WithMany(x => x.E3S);
}
}

public class E1
{
[Key]
public int Id { get; set; }

public ICollection E2S { get; set; }
}

public class E2
{
[Key]
public int Id { get; set; }

public E1 E1 { get; set; }

public ICollection E3S { get; set; }
}

public class E3
{
[Key]
public int Id { get; set; }

public E2 E2 { get; set; }
}
```

### Stack traces

```text
System.InvalidOperationException
HResult=0x80131509
Message=The LINQ expression 'x => x.y.E3S' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessSelectMany(NavigationExpansionExpression source, LambdaExpression collectionSelector, LambdaExpression resultSelector)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](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__DisplayClass11_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToQueryString(IQueryable source)
at ConsoleApp1.Program.Main() in D:\Work\Projekty\.Net\ConsoleApp1\Program.cs:line 20
```

### Verbose output

```text

```

### EF Core version

10.0.4

### Database provider

Any

### Target framework

.NET 10

### Operating system

_No response_

### IDE

VS2026

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.