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

Group by with linked objects #25460

Closed
kostazol opened this issue Aug 9, 2021 · 1 comment
Closed

Group by with linked objects #25460

kostazol opened this issue Aug 9, 2021 · 1 comment

Comments

@kostazol
Copy link

kostazol commented Aug 9, 2021

When I use this code everything is OK, and I get normal query with min operator.

var query = _dbSet.Where(p => p.Device.Connection.ChannelTypeId == channelTypeId
            && p.Device.Connection.WorkStationId == workStationId
            && p.CSDmodem != null
            && p.CSDmodem.PhoneNumber != "89999999999")
                .GroupBy(p => new { p.CSDmodem.PhoneNumber, p.Device.Connection.Node.Abonent.Account.Timezone.DiffWithUTC })
                .Select(g => new { PhoneNumber = g.Key.PhoneNumber, DownloadingStartHour = g.Key.DiffWithUTC, WorkStationId = g.Min(p => p.DeviceId), ChannelTypeId = channelTypeId });
                
            var pointsList = await query.ToListAsync();

But when I use this code, I get exception:

var query = _dbSet.Where(p => p.Device.Connection.ChannelTypeId == channelTypeId
            && p.Device.Connection.WorkStationId == workStationId
            && p.CSDmodem != null
            && p.CSDmodem.PhoneNumber != "89999999999")
                .GroupBy(p => new { p.CSDmodem.PhoneNumber, p.Device.Connection.Node.Abonent.Account.Timezone.DiffWithUTC })
                .Select(g => new { PhoneNumber = g.Key.PhoneNumber, DownloadingStartHour =  g.Key.DiffWithUTC, WorkStationId = g.Min(p => p.Device.Connection.WorkStationId), ChannelTypeId = channelTypeId });
                
            var pointsList = await query.ToListAsync();

There is exception text:
System.InvalidOperationException: The LINQ expression 'GroupByShaperExpression:\r\nKeySelector: new { \r\n PhoneNumber = c.PhoneNumber, \r\n DiffWithUTC = t.utc\r\n }, \r\nElementSelector:EntityShaperExpression: \r\n EntityType: Point\r\n ValueBufferExpression: \r\n ProjectionBindingExpression: EmptyProjectionMember\r\n IsNullable: False\r\n\r\n .Min(p => p.Device.Connection.WorkStationId)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.\r\n at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)\r\n at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TranslateInternal(Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.VisitMemberAssignment(MemberAssignment memberAssignment)\r\n at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.VisitMemberInit(MemberInitExpression memberInitExpression)\r\n at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression selectExpression, Expression expression)\r\n at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQueryExpression source, LambdaExpression selector)\r\n at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)\r\n at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)\r\n at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_01.<ExecuteAsync>b__0()\r\n at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetAsyncEnumerator(CancellationToken cancellationToken)\r\n at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable1.GetAsyncEnumerator()\r\n at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)\r\n at EtHangfire.DataAccess.Repositories.PointRepository.GetCSDmodemsForDownloadingAsync(Int32 channelTypeId, Int32 workStationId) in D:\1_git_repositories\energotronika_backend\Hangfire\Hangfire.DataAccess\Repositories\PointRepository.cs:line 83

EF Core version: 5.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system:
IDE: Visual Studio 2019 16.10.1

@smitpatel
Copy link
Member

Duplicate of #22609

@smitpatel smitpatel marked this as a duplicate of #22609 Aug 9, 2021
@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