Dear, first, excuse my English.
I found a similar error.
Follow steps to simuate:
C#
var query = db.Person
.Include(p => p.PersonType)
.ThenInclude(pt => pt.Type)
.Include(a => a.PersonAddress)
.Where(p => p.Active)
.GroupBy(p => new { item = 1 }); // => THROW EXCEPTION INDEX OUT OF RANGE
I'm using EF 2.0.1 version
Stack
em System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
em Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.BindSubqueryProjectionIndex(Int32 projectionIndex, IQuerySource querySource)
em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
em System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
em Remotion.Linq.Parsing.ThrowingExpressionVisitor.Visit(Expression expression)
em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visit(Expression expression)
em Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, Int32 index)
em Remotion.Linq.Clauses.OrderByClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
em Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection1 bodyClauses, QueryModel queryModel) em Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel) em Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) em Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitSubQuery(SubQueryExpression expression) em Remotion.Linq.Clauses.Expressions.SubQueryExpression.Accept(ExpressionVisitor visitor) em System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitLambdaT
em System.Linq.Expressions.Expression1.Accept(ExpressionVisitor visitor) em System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) em System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) em System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMethodCall(MethodCallExpression node) em System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) em System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) em System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node) em System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor) em System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitLambdaT
em System.Linq.Expressions.Expression1.Accept(ExpressionVisitor visitor) em System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) em System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) em System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) em Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMethodCall(MethodCallExpression node) em System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) em System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) em Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection) em Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.HandleGroup(EntityQueryModelVisitor entityQueryModelVisitor, GroupResultOperator groupResultOperator, QueryModel queryModel) em Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.<>c.<.cctor>b__33_11(EntityQueryModelVisitor v, ResultOperatorBase r, QueryModel q) em Microsoft.EntityFrameworkCore.Query.ResultOperatorHandler.HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel) em Microsoft.EntityFrameworkCore.Query.Internal.RelationalResultOperatorHandler.HandlerContext.EvalOnClient(Boolean requiresClientResultOperator) em Microsoft.EntityFrameworkCore.Query.Internal.RelationalResultOperatorHandler.HandleGroup(HandlerContext handlerContext) em Microsoft.EntityFrameworkCore.Query.Internal.RelationalResultOperatorHandler.HandleResultOperator(EntityQueryModelVisitor entityQueryModelVisitor, ResultOperatorBase resultOperator, QueryModel queryModel) em Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, Int32 index) em Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitResultOperator(ResultOperatorBase resultOperator, QueryModel queryModel, Int32 index) em Remotion.Linq.QueryModelVisitorBase.VisitResultOperators(ObservableCollection1 resultOperators, QueryModel queryModel)
em Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
em Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
em Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
em Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutorTResult
em Microsoft.EntityFrameworkCore.Storage.Database.CompileQueryTResult
--- Fim do rastreamento de pilha do local anterior onde a exce莽茫o foi gerada ---
em Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCoreTResult em Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_01.b__0()
em Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCoreTFunc em Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryTResult
em Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteTResult
em Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteTResult
em Remotion.Linq.QueryableBase1.GetEnumerator() em System.Linq.SystemCore_EnumerableDebugView1.get_Items()
Another aproach
```C#
var temp1 = query
.GroupBy(t => new { item = 1 })
.ToList(); //INDEX OUT OF RANGE
var temp2 = query
.GroupBy(t => new { item = 1 }.item)
.ToList(); //WORKS FINE
var temp3 = query
.GroupBy(t => 1)
.ToList(); //INDEX OUT OF RANGE
var temp4 = query
.OrderBy(t => new { item = 1 })
.ToList(); //INDEX OUT OF RANGE
var temp5 = query
.OrderBy(t => new { item = 1 }.item)
.ToList(); //WORKS FINE
var temp6 = query
.OrderBy(t => 1)
.ToList(); //INDEX OUT OF RANGE
```
Simplified Repro:
```C#
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace EFSampleApp
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new MyContext())
{
// Recreate database
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Seed database
db.SaveChanges();
}
using (var db = new MyContext())
{
// Run queries
var query = db.Person
.Include(p => p.PersonType)
.OrderBy(p => 1)
.ToList();
}
Console.WriteLine("Program finished.");
}
}
public class MyContext : DbContext
{
private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);
// Declare DBSets
public DbSet<Person> Person { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Select 1 provider
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
//.UseSqlite("filename=_modelApp.db")
//.UseInMemoryDatabase(databaseName: "_modelApp")
.EnableSensitiveDataLogging()
.UseLoggerFactory(LoggerFactory);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure model
}
}
public class Person
{
public int Id { get; set; }
public List<PersonType> PersonType { get; set; }
}
public class PersonType
{
public int Id { get; set; }
public Person Person { get; set; }
}
}
```
Also running into this, with a similarly simple use-case.
Full code to reproduce, very similar to smitpatel's, except using a .Select instead of .Include.
class Program
{
public static void Main()
{
using (var db = new PhotosContext())
{
db.Database.EnsureCreated();
db.Users
.OrderBy(u => 1)
.Select(u => u.Pictures)
.ToList();
}
}
}
public class PhotosContext : DbContext
{
public DbSet<Pictures> Pictures { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer("Server=(local);Initial Catalog=_IndexOutOfRangeRepro;Integrated Security=True");
}
public class User
{
public int Id { get; set; }
public virtual ICollection<Pictures> Pictures { get; set; }
}
public class Pictures
{
public int Id { get; set; }
public virtual User User { get; set; }
}
Removing either the OrderBy or .Select(u => u.Pictures) runs without the error. Running via LINQPad (UserQuery):
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.BindSubqueryProjectionIndex(Int32 projectionIndex, IQuerySource querySource)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Remotion.Linq.Parsing.ThrowingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, Int32 index)
at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, 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.RelationalEntityQueryableExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.VisitLambda[T](Expression`1 node)
at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)
at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression 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.CreateQueryExecutor[TResult](QueryModel queryModel)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_0`1.<Execute>b__0()
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..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at UserQuery.Program.Main() in R:\Temp\LINQPad5\_czoxpura\fvqjws\LINQPadQuery.cs:line 9
Using Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer version 2.0.1. Unfortunately, using the newest prerelease I'm aware of (2.0.1-rtm-203) has the same result.
Selecting a navigation utilizes the same code path as include hence same error.
I see - is there any workaround for this? Currently this is preventing me from updating from 1.1.2. I don't think this is a particularly isolated edge-case; ordering a collection and selecting a navigation property has to be pretty common.
I've tried explicitly defining the relationships in OnModelCreating, but still no dice. Maybe I'm missing some other kind of configuration though. Please feel free to call me stupid!
@ScottKaye this scenario should work as long as you order by something that is not a constant - e.g.
var query = db.Person
.Include(p => p.PersonType)
.OrderBy(p => p.Id)
.ToList();
To anyone experiencing this, taking the constant out to a variable works fine:
c#
var zero = 0;
var query = db.Person
.Include(p => p.PersonType)
.OrderBy(p => zero)
.ToList();
Just make sure the variable (zero) is not a constant.
Any notice for this?
@adrianotrentim - There is already PR out with fix for this. It will be fixed in 2.1 release.
fixed in 3902d943a489107d9e2336a434ecf489376d0516
I am still encountering this issue. What am I doing wrong?
Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.BindSubqueryProjectionIndex(Int32 projectionIndex, IQuerySource querySource)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
The issue is caused by two thing that I can see:
OrderBy + ThenBy using the same column.var results = context.Cat
.OrderBy(x => x.MeowLoudness)
.ThenBy(x => x.MeowLoudness)
.Select(x => new CatViewModel()
{
Id = x.Id,
Name = x.Name,
Breed = new CatBreedViewModel() {
Id = x.Breed.Id,
BreedName = x.Breed.BreedName,
Cats = x.Breed.Cats.Select(y => new CatViewModel()
{
Id = y.Id,
MeowLoudness = y.MeowLoudness,
Name = y.Name,
TailLength = y.TailLength
}).ToList()
},
MeowLoudness = x.MeowLoudness,
TailLength = x.TailLength
})
.ToList();
@VictorioBerra can you post a full repro project? I was trying to reverse engineer the model from the query you provided and I'm seeing the correct behavior. I came up with the following model:
public class CatViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int MeowLoudness { get; set; }
public int TailLength { get; set; }
public CatBreedViewModel Breed { get; set; }
}
public class CatBreedViewModel
{
public int Id { get; set; }
public string BreedName { get; set; }
public List<CatViewModel> Cats { get; set; }
}
public class Cat
{
public int Id { get; set; }
public string Name { get; set; }
public int MeowLoudness { get; set; }
public int TailLength { get; set; }
public CatBreed Breed { get; set; }
}
public class CatBreed
{
public int Id { get; set; }
public string BreedName { get; set; }
public List<Cat> Cats { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Cat> Cats { get; set; }
public DbSet<CatBreed> CatBreeds { get; set; }
}
Wasn't this closed as fixed 6 months ago? There should be a repro project in my most recent post.
@VictorioBerra sorry, I somehow missed the link to the full repo. Bug was fixed long time ago, but your post was created later so I wanted to make sure the bug fix was complete and we didn't miss something. Using the full repro you provided I was able to reproduce it in 2.0.0 and in 2.1.3. However everything works correctly on the current bits (2.2.0 preview2)
@maumar thanks a lot for following up I appreciate it.
Most helpful comment
fixed in 3902d943a489107d9e2336a434ecf489376d0516