Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using new[] in select query gives exception .NET 8 #32331

Closed
Ogglas opened this issue Nov 17, 2023 · 2 comments · Fixed by #32460
Closed

Using new[] in select query gives exception .NET 8 #32331

Ogglas opened this issue Nov 17, 2023 · 2 comments · Fixed by #32460
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@Ogglas
Copy link

Ogglas commented Nov 17, 2023

File a bug

Include your code

I could not find anything about this and therefore decided to post this.

This code worked with .NET 7 and Microsoft.EntityFrameworkCore.SqlServer Version 7.0.13:

Updated = p.ThreatAndCountermeasures.Count() > 0 ? new[] { p.Updated, p.ThreatAndCountermeasures.Max(tac => tac.Updated) }.Max() : p.Updated,

With .NET 8 and Microsoft.EntityFrameworkCore.SqlServer Version 8.0.0 I get the following exception:

Query root of type 'InlineQueryRootExpression' wasn't handled by provider code. This issue happens when using a provider specific method on a different provider where it is not supported.

If I use this code it works:

Updated = p.ThreatAndCountermeasures.Count() > 0 ? new List<DateTime> { p.Updated, p.ThreatAndCountermeasures.Max(tac => tac.Updated) }.Max() : p.Updated,

https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/breaking-changes

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 8.0
Operating system: Windows 11 Pro OS build 22621.2506
IDE: Microsoft Visual Studio 2022 (64-bit) - Current Version 17.8.0

@roji
Copy link
Member

roji commented Nov 17, 2023

Minimal repro query:

_ = context.Blogs
    .Select(b => new[] { b.Updated, b.Posts.Max(p => p.Updated) }.Max())
    .ToList();
Full repro source code
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

_ = context.Blogs
    .Select(b => new[] { b.Updated, b.Posts.Max(p => p.Updated) }.Max())
    .ToList();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();
}

public class Blog
{
    public int Id { get; set; }
    public DateTime Updated { get; set; }
    public DateTime Updated2 { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public DateTime Updated { get; set; }

    public Blog Blog { get; set; }
}

In 7.0 this indeed translated, with the outer Max() evaluated client-side:

SELECT [b].[Updated], (
    SELECT MAX([p].[Updated])
    FROM [Post] AS [p]
    WHERE [b].[Id] = [p].[BlogId])
FROM [Blogs] AS [b]

This only fails when a subquery is inside the inline collection. Given the following LINQ query:

_ = context.Blogs
    .Select(b => new[] { b.Updated, b.Updated2 }.Max())
    .ToList();

... 8.0 produces the following, translating the outer Max() to SQL:

SELECT (
    SELECT MAX([v].[Value])
    FROM (VALUES ([b].[Updated]), ([b].[Updated2])) AS [v]([Value]))
FROM [Blogs] AS [b]

... whereas 7.0 again evaluates client-side:

SELECT [b].[Updated], [b].[Updated2]
FROM [Blogs] AS [b]

Opened #32332 to improve the 8.0 translation to use GREATEST().

@ajcvickers ajcvickers added this to the 8.0.x milestone Nov 29, 2023
roji added a commit to roji/efcore that referenced this issue Nov 30, 2023
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 30, 2023
roji added a commit to roji/efcore that referenced this issue Nov 30, 2023
roji added a commit to roji/efcore that referenced this issue Nov 30, 2023
@roji
Copy link
Member

roji commented Nov 30, 2023

Reopening to consider servicing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants