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 Select OrderBy produces invalid SQL near the keyword 'AS' #13438

Closed
jag43 opened this issue Sep 28, 2018 · 1 comment
Closed

GroupBy Select OrderBy produces invalid SQL near the keyword 'AS' #13438

jag43 opened this issue Sep 28, 2018 · 1 comment

Comments

@jag43
Copy link

jag43 commented Sep 28, 2018

I have a LINQ query

var result = await _context.TelemCalls.GroupBy(c => new
{
    JobId = c.CallStack.JobSupplier.Job.Id,
    StartDate = c.CallStack.JobSupplier.Created
})
.Select(g => new QueryResultModel()
{
    JobId = g.Key.JobId,
    StartDate = g.Key.StartDate,
    CallsMade = g.Count()
})
.OrderBy(r => r.StartDate)
.ToListAsync();

Which is producing incorrect SQL

SELECT [c.CallStack.JobSupplier.Job].[Id] AS [JobId], [c.CallStack.JobSupplier].[Created] AS [StartDate], COUNT(*) AS [CallsMade]
FROM [telem_Calls] AS [c]
INNER JOIN [telem_CallStacks] AS [c.CallStack] ON [c].[CallStackId] = [c.CallStack].[Id]
INNER JOIN [telem_JobSuppliers] AS [c.CallStack.JobSupplier] ON [c.CallStack].[JobSupplierId] = [c.CallStack.JobSupplier].[Id]
INNER JOIN [telem_Jobs] AS [c.CallStack.JobSupplier.Job] ON [c.CallStack.JobSupplier].[JobId] = [c.CallStack.JobSupplier.Job].[Id]
GROUP BY [c.CallStack.JobSupplier.Job].[Id], [c.CallStack.JobSupplier].[Created]
ORDER BY [c.CallStack.JobSupplier].[Created] AS [StartDate]

Note the AS [StartDate] at the end

This is producing an exception

Exception message: Incorrect syntax near the keyword 'AS'.
Stack trace:at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__122_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 where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)

Steps to reproduce

I have created a sample project here. Let me know if you need anything more.

Further technical details

EF Core version: Microsoft.EntityFrameworkCore 2.1.3
Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.1.3
Operating system: Windows 10 Pro Version 10.0.17134 Build 17134
IDE: Visual Studio 2017 15.8.5

@maumar
Copy link
Contributor

maumar commented Sep 28, 2018

dupe of #12826

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants