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() Throw an error when used with 2 level of .FirstOrDefault() #14172

Closed
plaramee1 opened this issue Dec 14, 2018 · 2 comments
Closed

Comments

@plaramee1
Copy link

plaramee1 commented Dec 14, 2018

EF core Throw an error when I use .FirstOrDefault() in a groupBy.

in the example below the code works:

 var OriginForms = db.OriginForms
   .OrderByDescending(a => a.Id)
   .Select(a => new
   {
     a.Id,
     a.Name,
     FieldCount = a.OriginFormDatas
       .GroupBy(x => x.Revision)
       .OrderByDescending(x => x.Key)
       .FirstOrDefault()
       .Select(x => x.OriginFormFieldPositions.Count())
   }).ToList();

 OriginForms.ForEach(of =>
 {
   Console.WriteLine($"ID:{of.Id},FC:{of.FieldCount.FirstOrDefault()}");
 });

This code will throw

 var OriginForms = db.OriginForms
   .OrderByDescending(a => a.Id)
   .Select(a => new
   {
     a.Id,
     a.Name,
     FieldCount = a.OriginFormDatas
       .GroupBy(x => x.Revision)
       .OrderByDescending(x => x.Key)
       .FirstOrDefault()
       .Select(x => x.OriginFormFieldPositions.Count()).DefaultIfEmpty(0).FirstOrDefault()
   }).ToList();

 OriginForms.ForEach(of =>
 {
   Console.WriteLine($"ID:{of.Id},FC:{of.FieldCount}");
 });
Exception message:
Unhandled Exception: System.ArgumentException: Value does not fall within the expected range.
   at System.SharedTypeExtensions.GetSequenceType(Type type)
   at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, 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.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visit(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitUnary(UnaryExpression expression)
   at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression expression)
   at Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionVisitors.Internal.SqlServerSqlTranslatingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
   at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visit(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression)
   at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
   at Remotion.Linq.Parsing.RelinqExpressionVisitor.VisitNew(NewExpression expression)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.VisitNew(NewExpression newExpression)
   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.CreateQueryExecutor[TResult](QueryModel queryModel)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

Further technical details

EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: MacOS 10.14.2
IDE: VisualStudio Code 1.30.0

@smitpatel
Copy link
Member

Duplicate of #12089

@smitpatel smitpatel marked this as a duplicate of #12089 Dec 14, 2018
@plaramee1
Copy link
Author

well I just found that we can call .ToArray() and take the first index and that work....

.Select(x => x.OriginFormFieldPositions.Count()).Take(1).ToArray()[0]

Ill leave a comment in my code to fix this when 3.0 is release !

@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