Get the values of Group by using MyGroup.ToList() Throw exception, Must select First
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.1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.System.Collections.Generic.IAsyncEnumerable
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
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
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()
This is working correctly in latest dev for sync & async both code paths. Can you try latest nightlies to verify fixed?