efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Get the values of Group by using MyGroup.ToList() Throw exception, Must select First

Open AhmedRagheb opened this issue 8 years ago • 3 comments

Get the values of Group by using grp.ToList() Throw exception I must Select First

Exception message:
Stack trace:

Expression of type 'System.Linq.IAsyncGrouping2[x,y]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable1[y]' of method 'System.Collections.Generic.List1[y] ToList[y](System.Collections.Generic.IEnumerable1[y])' Parameter name: arg0 at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index) at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0) at System.Linq.Expressions.MethodCallExpression1.Rewrite(Expression instance, IReadOnlyList1 args) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.MemberAccessBindingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node) at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node) at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection1 nodes, Func2 elementVisitor) at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node) at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor[TResult](QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass24_01.b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.System.Collections.Generic.IAsyncEnumerable.GetEnumerator() at System.Linq.AsyncEnumerable.<Aggregate_>d__6`3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

Steps to reproduce

var q =  await (
              from t1 in _context.T1
	      join t2 in _context.T2 on t1.Id equals t2.Id
              group t2 by t1 into grp
              select new MyModel
	      {
		  T1 = grp.Key,
		  T2s = grp.ToList() // THROW exception .. should use  grp..Select(x => x).ToList() 
	      }
            ).ToListAsync();

Further technical details

EF Core version: 2.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2017 15.4

AhmedRagheb avatar Jan 17 '18 08:01 AhmedRagheb

I don't understand the need for this MyModel, why not just return the query as a dictionary?

await (
              from t1 in _context.T1
	      join t2 in _context.T2 on t1.Id equals t2.Id
              group t2 by t1 into grp
).ToDictionaryAsync((grp)=>grp.Key, (grp)=>grp);

I guess that forces the select though

Tarig0 avatar Jan 17 '18 16:01 Tarig0

I need it as List to pass it to another method in my business flow .. this is not the Issue :) The issue in grp.ToList()

AhmedRagheb avatar Jan 18 '18 07:01 AhmedRagheb

This is working correctly in latest dev for sync & async both code paths. Can you try latest nightlies to verify fixed?

smitpatel avatar Feb 15 '18 01:02 smitpatel