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

GroupBy over complex types #33491

Closed
Tracked by #31238
sexyDaniel opened this issue Apr 8, 2024 · 1 comment · Fixed by #33493
Closed
Tracked by #31238

GroupBy over complex types #33491

sexyDaniel opened this issue Apr 8, 2024 · 1 comment · Fixed by #33493
Assignees
Labels
area-groupby area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@sexyDaniel
Copy link

sexyDaniel commented Apr 8, 2024

Include your code

SimpleRepo1.zip
When I try to group by a property that has a complexProperty, this complexProperty is not involved in the final sql query. (there were no similar problems in ef 6 )

var result = context.Companies.GroupBy(x => x.Manager)
    .Select(x => new { Phone = x.Key.Phone.Value, Name = x.Key.Name });

Generated sql query (Ef6 made similar requests via distinct)

SELECT [m].[Phone_Value] AS [Phone], [m].[Name]
FROM [Companies] AS [c]
INNER JOIN [Manager] AS [m] ON [c].[ManagerId] = [m].[Id]
GROUP BY [m].[Id], [m].[Name]

if you use Сount(), the error is the same

var result = context.Companies.GroupBy(x => x.Manager)
    .Select(x => new { Phone = x.Key.Phone.Value, Count = x.Count() });

Include stack traces

Microsoft.Data.SqlClient.SqlException: 'Column 'Manager.Phone_Value' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.'
      at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbD
ataReaderAsync>b__211_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__18.MoveNext()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__18.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.<InitializeReaderAsync>d__21.MoveNext()
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.<MoveNextAsync>d__20.MoveNext()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__67`1.MoveNext()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__67`1.MoveNext()
at Program.<<Main>$>d__0.MoveNext() in C:\Users\user\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 9

Include provider and version information

EF Core version: 8.0.3
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.9.2)

@roji
Copy link
Member

roji commented Apr 9, 2024

Note that we also don't translate GroupBy over an owned entity (#29018); doing that doesn't seem useful since entity identity semantics apply, and so the group can always only contain one item.

Minimal repro
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

_ = await context.Blogs
    .GroupBy(b => b.Address)
    .Select(g => new { g.Key, Count = g.Count() })
    .ToListAsync();

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // modelBuilder.Entity<Blog>().OwnsOne(b => b.Address);
        modelBuilder.Entity<Blog>().ComplexProperty(b => b.Address);
    }
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Address Address { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
}

roji added a commit to roji/efcore that referenced this issue Apr 9, 2024
@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 Apr 9, 2024
roji added a commit to roji/efcore that referenced this issue Apr 9, 2024
roji added a commit that referenced this issue Apr 9, 2024
@roji roji modified the milestones: 9.0.0-preview4, 9.0.0 Apr 9, 2024
@roji roji changed the title A complex property(own type) is not included in the group by Cannot do GroupBy over a complex type Apr 9, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview4 Apr 29, 2024
@roji roji changed the title Cannot do GroupBy over a complex type GroupBy over complex types Sep 23, 2024
@roji roji modified the milestones: 9.0.0-preview4, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-groupby area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants