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

System.ArgumentException when using synchronuous LINQ aggregate methods in async GroupBy selection #12849

Closed
olivierr91 opened this issue Jul 31, 2018 · 5 comments
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported type-bug

Comments

@olivierr91
Copy link

olivierr91 commented Jul 31, 2018

OrderByDescending within the Select clause causes EF Core 2.1 to crash:

MyContext.InventoryEntries
     .GroupBy(ie => new { ie.Item, ie.Warehouse })
     .Select(g => new InventorySummaryEntry() {
          ActualPackageQuantity = g.Sum(ie => ie.ActualPackageQuantity),
          Entries = g.OrderByDescending(e => e.PurchaseOrderItem.PurchaseOrder.OpenedOn).ToList() //Line here is causing problem
     })
     .ToListAsync();

Exception within EF Core:

System.ArgumentException: 'Expression of type 'System.Collections.Generic.IAsyncEnumerable1[Sitka.WebApp.Server.Core.Domain.Inventory.InventoryEntry]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable1[Sitka.WebApp.Server.Core.Domain.Inventory.InventoryEntry]' of method 'System.Collections.Generic.List1[Sitka.WebApp.Server.Core.Domain.Inventory.InventoryEntry] ToList[InventoryEntry](System.Collections.Generic.IEnumerable1[Sitka.WebApp.Server.Core.Domain.Inventory.InventoryEntry])''

While the following LINQ expression (with no OrderByDescending) does not throw:

MyContext.InventoryEntries
               .GroupBy(ie => new { ie.Item, ie.Warehouse })
               .Select(g => new InventorySummaryEntry() {
                   ActualPackageQuantity = g.Sum(ie => ie.ActualPackageQuantity),
                   Entries = g.ToList() //OK
               })
               .ToListAsync();

Stack Trace:

System.Linq.Expressions.dll!System.Linq.Expressions.Expression.Field(System.Linq.Expressions.Expression expression, System.Reflection.FieldInfo field)	Unknown
 	System.Linq.Expressions.dll!System.Linq.Expressions.Expression.MakeMemberAccess(System.Linq.Expressions.Expression expression, System.Reflection.MemberInfo member)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ShiftMemberAccess(System.Linq.Expressions.Expression targetExpression, System.Linq.Expressions.Expression currentExpression)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.IntroduceTransparentScope(Remotion.Linq.Clauses.IQuerySource querySource, Remotion.Linq.QueryModel queryModel, int index, System.Type transparentIdentifierType)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitGroupJoinClause(Remotion.Linq.Clauses.GroupJoinClause groupJoinClause, Remotion.Linq.QueryModel queryModel, int index)	Unknown
 	Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitGroupJoinClause(Remotion.Linq.Clauses.GroupJoinClause groupJoinClause, Remotion.Linq.QueryModel queryModel, int index)	Unknown
 	Remotion.Linq.dll!Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(System.Collections.ObjectModel.ObservableCollection<Remotion.Linq.Clauses.IBodyClause> bodyClauses, Remotion.Linq.QueryModel queryModel)	Unknown
 	Remotion.Linq.dll!Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.LiftSubQuery(Remotion.Linq.Clauses.IQuerySource querySource, Remotion.Linq.Clauses.Expressions.SubQueryExpression subQueryExpression)	Unknown
 	Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileMainFromClauseExpression(Remotion.Linq.Clauses.MainFromClause mainFromClause, Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(Remotion.Linq.Clauses.MainFromClause fromClause, Remotion.Linq.QueryModel queryModel)	Unknown
 	Remotion.Linq.dll!Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>(Remotion.Linq.QueryModel queryModel)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>(object cacheKey, System.Func<System.Func<Microsoft.EntityFrameworkCore.Query.QueryContext, System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>> compiler)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>(System.Linq.Expressions.Expression query)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>.System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>.GetEnumerator()	Unknown
 	System.Interactive.Async.dll!System.Linq.AsyncEnumerable.Aggregate_<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>(System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> source, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> seed, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> accumulator, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> resultSelector, System.Threading.CancellationToken cancellationToken)	Unknown
 	System.Private.CoreLib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start<System.Linq.AsyncEnumerable.<Aggregate_>d__6<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>>(ref System.Linq.AsyncEnumerable.<Aggregate_>d__6<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> stateMachine) Line 930	C#
 	System.Interactive.Async.dll!System.Linq.AsyncEnumerable.Aggregate_<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>(System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> source, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> seed, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> accumulator, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> resultSelector, System.Threading.CancellationToken cancellationToken)	Unknown
 	System.Interactive.Async.dll!System.Linq.AsyncEnumerable.Aggregate<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>(System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> source, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> seed, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> accumulator, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> resultSelector, System.Threading.CancellationToken cancellationToken)	Unknown
 	System.Interactive.Async.dll!System.Linq.AsyncEnumerable.Aggregate<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>>(System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> source, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> seed, System.Func<System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>, Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry, System.Collections.Generic.List<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>> accumulator, System.Threading.CancellationToken cancellationToken)	Unknown
 	System.Interactive.Async.dll!System.Linq.AsyncEnumerable.ToList<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>(System.Collections.Generic.IAsyncEnumerable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> source, System.Threading.CancellationToken cancellationToken)	Unknown
 	Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry>(System.Linq.IQueryable<Sitka.WebApp.Server.Core.Domain.Inventory.InventorySummaryEntry> source, System.Threading.CancellationToken cancellationToken)	Unknown
@ajcvickers
Copy link
Contributor

@orobert91 Can you please also post the entity types used, since the specific layout of navigation properties, etc. may impact our ability to reproduce the behavior you are seeing.

@olivierr91
Copy link
Author

olivierr91 commented Aug 3, 2018

@ajcvickers You should be able to reproduce it with the same example that @smitpatel made for #12852 by changing the inner g.ToList() within the select clause to the more complexe LINQ query above. If not let me know.

@ajcvickers ajcvickers added this to the 3.0.0 milestone Aug 3, 2018
@AndriySvyryd AndriySvyryd changed the title System.ArgumentException when using synchronuous LINQ agregate methods in async GroupBy selection System.ArgumentException when using synchronuous LINQ aggregate methods in async GroupBy selection Nov 30, 2018
@divega divega modified the milestones: 3.0.0, Backlog Jun 21, 2019
@smitpatel smitpatel removed their assignment Jun 24, 2019
@PawelGerr
Copy link

PawelGerr commented Jun 26, 2019

Just stumbled upon the same exception in one of my projects but my query is slightly different.

The issue is very easy to reproduce.

var query = Context.ProductGroups
                   .Select(g => new
                                {
                                   g.Id,
                                   Products = g.Products.ToList()
                                })
                   .GroupBy(g => g.Id);

query.ToList(); // works

await query.ToListAsync(); // throws ArgumentException

Github: GroupBy_Issues_Demos.cs

@d1mnewz
Copy link

d1mnewz commented Oct 31, 2019

We experience the same issue. Very weird.

			var pagedResult = source.Skip(offset)
				.Take(count)
				.GroupBy(x => 1)
				.Select(c => new { count = source.Count(), items = c.ToList() })
				.SingleOrDefaultAsync();

@smitpatel
Copy link
Contributor

This issue is tracking bug in pipeline when after doing group by, whole group is being materialized. That requires client eval of group by (i.e. what happened in 2.x release). Since 3.0 does not support this, the issue is no longer relevant. Implicitly client eval'ing group by is being tracked at #17068, if we ever fixed that, we will support this scenario too.

@smitpatel smitpatel removed this from the Backlog milestone Oct 31, 2019
@ajcvickers ajcvickers added the closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. label Mar 10, 2022
@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
Labels
closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported type-bug
Projects
None yet
Development

No branches or pull requests

6 participants