ApplyQueryHints throws Collection was modified; enumeration operation may not execute
See original GitHub issueI use _context.NextQueryHints.Add(SqlServerTools.Sql.OptionRecompile);
in several part of my code. and I register the connection as scoped in DI.
sometimes when my services are under load, I get this exception
Exception message:
Collection was modified; enumeration operation may not execute.
Stack trace:
`"trace":" at System.Collections.Generic.List`1.Enumerator.MoveNextRare()\r\n at LinqToDB.SqlProvider.BasicSqlBuilder.ApplyQueryHints(String sql, List`1 queryHints)\r\n at LinqToDB.Data.DataConnection.InitCommand(CommandType commandType, String sql, DataParameter[] parameters, List`1 queryHints, Boolean withParameters)\r\n at LinqToDB.Data.DataConnection.QueryRunner.ExecuteReaderAsync(CancellationToken cancellationToken)\r\n at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)\r\n at LinqToDB.Linq.ExpressionQuery`1.GetForEachAsync(Action`1 action, CancellationToken cancellationToken)\r\n at LinqToDB.AsyncExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken token)\r\n at WebApi.Commands.MarketInfo.StockVolumeChartReadingCommand.StockVolumeChartReadingHandler.Handle(StockVolumeChartReadingCommand request, CancellationToken cancellationToken) in D:\\Workspaces\\New Mdp\\WebApi\\Commands\\MarketInfo\\StockVolumeChartReadingCommand.cs:line 69\r\n at MediatR.Pipeline.RequestExceptionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n at MediatR.Pipeline.RequestExceptionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n at WebApi.Controllers.MarketInfoController.StockVolumeChartReadingCommand(StockVolumeReadingChartRequest request) in D:\\Workspaces\\New Mdp\\WebApi\\Controllers\\MarketInfoController.cs:line 31\r\n at lambda_method1763(Closure , Object )\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location ---\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\r\n at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at WebApi.Middlewares.ApiExceptionHandlingMiddleware.Invoke(HttpContext context) in D:\\Workspaces\\New Mdp\\WebApi\\Middlewares\\ApiExceptionHandlingMiddleware.cs:line 37",`
there is one part of my code that I use OptionRecompile :
var query = _context.FinalScreeners.With("NoLock").AsQueryable();
query = query.Where(x => x.TradeDate >= request.Request.FromDate);
query = query.Where(x => x.TradeDate.Date <= request.Request.ToDate);
_context.NextQueryHints.Add(SqlServerTools.Sql.OptionRecompile);
return await query.Select(x => new StockVolumeReadingChartResponse
{
TradeDate = x.TradeDate,
Isin = x.Isin,
}).SetOrdering(request.Request.Sort).ToListAsync(token: cancellationToken);
Environment details
linq2db version: 3.2.3 Database Server: Sql Server 2019 Database Provider: ? Operating system: Windows Server 2019 .NET Framework: Net 5.0.2
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Collection was modified; enumeration operation may not ...
So the issue behind this error is, we can not modify the list/dictionary while we are looping through the same. One of the...
Read more >Why the error Collection was modified enumeration ...
This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or...
Read more >Collection Was Modified Enumeration Operation May Not ...
The collection was modified enumeration operation may not execute exception occurs when you modify the collection while looping through the same.
Read more >Collection was modified; enumeration operation may not ...
Solution 1 – If you're removing items, use RemoveAll() · Solution 2 – If you're adding items, put them in a temp and...
Read more >enumeration operation may not execute. At foreach
Solution 2 You cannot enumerate a collection in a foreach block and modify the collection in the same block.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Never change
MappingSchema.Default
in that way, Create new MappingSchema and add to DataConnectionUpdate: I have another exception:
Collection was modified; enumeration operation may not execute
this time it accrues on fluent map class :this is fluentMap class :
I Add this class to my context this way :
and new StackTrace :
I emphasize again that this service works properly in most cases with this configuration. and now when I change my fluent map classes and remove all property configuration, The services work properly under load (till now)