Skip to content

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

Closed

Description

@Ogglas

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

Activity

  1. roji commented on Nov 17, 2023

    @roji
    Member

    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().

  2. added this to the 8.0.x milestone on Nov 29, 2023
  3. added a commit that references this issue on Nov 30, 2023
    907f202
  4. added 2 commits that reference this issue on Nov 30, 2023
    0c75aae
    d7d5a82
  5. added a commit that references this issue on Nov 30, 2023
    50a8ae5
  6. roji commented on Nov 30, 2023

    @roji
    Member

    Reopening to consider servicing.

  7. reopened this on Nov 30, 2023
  8. added 2 commits that reference this issue on Nov 30, 2023
    7470bcd
    59e1324
  9. modified the milestones: 8.0.x, 8.0.2 on Dec 5, 2023
  10. added 2 commits that reference this issue on Jan 3, 2024
    5119bb7
    cdbc432
  11. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions