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.ArgumentOutOfRangeException: 'Index was out of range. #1576

Closed
achilleaskar opened this issue Nov 30, 2021 · 10 comments
Closed

System.ArgumentOutOfRangeException: 'Index was out of range. #1576

achilleaskar opened this issue Nov 30, 2021 · 10 comments

Comments

@achilleaskar
Copy link

achilleaskar commented Nov 30, 2021

Steps to reproduce

It might be quite hard to reproduce because my project is very complex

The issue

I get a very strange error In one of my queries that works perfectly in the .Net Framework version of my app (there I use MySql.Data.EntityFramework)

The query is this:

var x = await Context.Personal_Bookings
  .Where(c =>
  (completed || c.Services.Any(s => (s.Allerretour && s.TimeReturn >= DateTime.Today) || s.TimeGo >= DateTime.Today))
  .Include(f => f.Customers).ThenInclude(s => s.Services)
  .Include(f => f.User)
  .Include(f => f.Payments)
  .Include(f => f.Partner)
  .ToListAsync();

The problem occurs in (completed || c.Services.Any(s => (s.Allerretour && s.TimeReturn >= DateTime.Today) || s.TimeGo >= DateTime.Today))

I get exception System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'

If I change it to (completed || c.Services.Any(s => s.TimeReturn >= DateTime.Today || s.TimeGo >= DateTime.Today)) it works fine. The problem is in using two conditions. Any idea why this happens?

Further technical details

MySQL version:
Operating system:
Pomelo.EntityFrameworkCore.MySql version: 6.0.0

@achilleaskar
Copy link
Author

Looks like the "Allerretour" causes the problem. It is a bool (not nullable) and in database it is type bit(1)

@mguinness
Copy link
Collaborator

Could be related to dotnet/efcore#26428.

@lauxjpn
Copy link
Collaborator

lauxjpn commented Nov 30, 2021

Could be related to dotnet/efcore#26428.

@achilleaskar Are you using query filters?

@achilleaskar
Copy link
Author

achilleaskar commented Nov 30, 2021

No only the where you can see. I also get the same problem in other application .Net 6 at this block of code

query = query
    .Where(t=> t.ExcursionDates.Any(d=> !includeDates || d.CheckOut >= DateTime.Today))
    .OrderBy(e => e.ExcursionDates.Where(d => d.CheckIn >= DateTime.Today)
    .OrderBy(ed => ed.CheckIn).FirstOrDefault().CheckIn);

var x = await query.Select(e => new ExcursionWithDatesDto
{
    ExcursionDates = !includeDates ? new List<ExcursionDateDto>() : e.ExcursionDates
        .Where(d => d.CheckOut >= DateTime.Today).Select(d => new ExcursionDateDto
    {
        Name = d.Name,
        Id = d.Id,
        NightStart = d.NightStart,
        CheckIn = !d.NightStart ? d.CheckIn : d.CheckIn.AddDays(1), //change here
        CheckOut = d.CheckOut
    }).ToList()
}).ToListAsync();

The fourth line of the end is the fault of this error. If I change it to CheckIn = d.CheckIn it works perfectly. I will try to debug it my self and come up for more info.

@vaital3000
Copy link

vaital3000 commented Dec 3, 2021

Hi!
I wanted to create new issue, but may be it related problem.

I reproduce same problem(System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')) in simple sample.

https://github.com/vaital3000/net6efMysqlProblem
https://github.com/vaital3000/net6efMysqlProblem/runs/4405172285

I faced this problem after migration to net6 with ef core 6 and Pomelo.EntityFrameworkCore.MySql 6.
An example shows that it works with 5 versions, but doesn't work with 6.

@mguinness
Copy link
Collaborator

@vaital3000 Could you try another database provider like SQLite or PostgreSQL to see if you have the same issue?

@achilleaskar
Copy link
Author

achilleaskar commented Dec 3, 2021

I searched for hours, downloaded both efcore & pomelo repo to dig into this one and I found out that it's a known efcore 6 issue that is fixed and will be ok at 6.0.1 release of ef core. So just wait for it...

@achilleaskar
Copy link
Author

@mguinness
Copy link
Collaborator

OK, thanks for verifying that it was related to dotnet/efcore#26428 that was previously mentioned.

@mguinness
Copy link
Collaborator

FYI for those impacted by this issue: EF Core 6.0.1 is now available from NuGet.

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

4 participants