Skip to content

GroupBy over complex types #33491

Closed

Description

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions