Click here to Skip to main content
15,512,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting this issue at stage env only after making deployment and getting error at response.

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.JoinIterator[TOuter,TInner,TKey,TResult](IEnumerable`1 outer, IEnumerable`1 inner, Func`2 outerKeySelector, Func`2 innerKeySelector, Func`3 resultSelector, IEqualityComparer`1 comparer)+MoveNext()
   at System.Linq.Enumerable.JoinIterator[TOuter,TInner,TKey,TResult](IEnumerable`1 outer, IEnumerable`1 inner, Func`2 outerKeySelector, Func`2 innerKeySelector, Func`3 resultSelector, IEqualityComparer`1 comparer)+MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.EnumerablePartition`1.ToList()
   at Soulmaker.Services.VideoService.GetClientDetailAsync(Int64 id, Int64 userId)
   at Soulmaker.API.Controllers.VideoController.GetClientDetails(Int32 id) in /src/Soulmaker.API/Controllers/VideoController.cs:line 184
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)


What I have tried:

Here is my code:

public async Task<VideoDetailsResponse> GetClientDetailAsync(long id, long userId)
       {
           var video = await _context.Videos
                                 .Include(x => x.CreatedByUser)
                                 .Include(x => x.Like)
                                 .Include(x => x.PublishedByUser)
                                 .Include(x => x.ModifiedByUser)
                                 .Include(x => x.Tags)
                                 .ThenInclude(x => x.Topic)
                                 .Include(x => x.AdminTags)
                                 .ThenInclude(x => x.AdminTag)
                                 .FirstOrDefaultAsync(x => !x.IsDeleted && !x.IsHidden && x.Id == id);

           var response = _mapper.Map<VideoDetailsResponse>(video);
           response.IsLiked = video.Like.Any(x => x.UserId == userId);

           var items = (from topicId in video.Tags.Select(x => x.TopicId)
                        join tag in _context.VideoTagLink on topicId equals tag.TopicId
                        join videoItem in _context.Videos
                                           .Include(x => x.CreatedByUser)
                                           .Include(x => x.Like)
                                           .Include(x => x.PublishedByUser)
                                           .Include(x => x.ModifiedByUser)
                                           .Include(x => x.Tags)
                                           .ThenInclude(x => x.Topic)
                                           .Include(x => x.AdminTags)
                                           .ThenInclude(x => x.AdminTag)
                                           .Where(x => !x.IsDeleted && !x.IsHidden)
                         on tag.VideoId equals videoItem.Id
                         where tag.VideoId != id && !videoItem.IsDeleted
                         select videoItem).Take(2).ToList();

           var relatedExperiences = new List<RelatedExpirienceResponse>();
           for ( int i = 0; i < items.Count; i++ )
           {
               var item = _mapper.Map<RelatedExpirienceResponse>(items[i]);
               item.IsLiked = items[i].Like.Any(x => x.UserId == userId);
               relatedExperiences.Add(item);
           }
           response.RealtedExpiriences.AddRange(relatedExperiences);

           return response;
       }
Posted
Updated 3-Dec-21 4:57am

1 solution

I suspect it's because you're trying to join an in-memory list to a database table.

Try:
C#
var topicIds = video.Tags.Select(x => x.TopicId).ToList();
var items = (from tag in _context.VideoTagLink.Where(t => topicIds.Contains(t.TopicId))
             join videoItem in _context.Videos
                 .Include(x => x.CreatedByUser)
                 .Include(x => x.Like)
                 .Include(x => x.PublishedByUser)
                 .Include(x => x.ModifiedByUser)
                 .Include(x => x.Tags)
                 .ThenInclude(x => x.Topic)
                 .Include(x => x.AdminTags)
                 .ThenInclude(x => x.AdminTag)
                 .Where(x => !x.IsDeleted && !x.IsHidden)
             on tag.VideoId equals videoItem.Id
             where tag.VideoId != id && !videoItem.IsDeleted
             select videoItem).Take(2).ToList();
 
Share this answer
 
Comments
BillWoodruff 4-Dec-21 0:44am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900