3

we've been getting this error lately and have no idea why, we are not modifying any collection, we even removed most foreach on the pages that we were getting the error. Here is the stacktrace of the error:

   Exception type: InvalidOperationException 
    Exception message: Collection was modified; enumeration operation may not execute.
   en System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   en System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   en System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   en System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   en System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
   en System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   en System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   en System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   en System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext()
   en System.Web.Mvc.FilterProviderCollection.<RemoveDuplicates>d__b.MoveNext()
   en System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   en System.Linq.Enumerable.<ReverseIterator>d__a0`1.MoveNext()
   en System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   en System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   en System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   en System.Web.Mvc.FilterInfo..ctor(IEnumerable`1 filters)
   en System.Web.Mvc.ControllerActionInvoker.GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
   en System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   en System.Web.Mvc.Controller.ExecuteCore()
   en System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   en System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   en System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   en System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   en System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   en System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Any help is appreciated.

Gabe
  • 84,912
  • 12
  • 139
  • 238
ryudice
  • 36,476
  • 32
  • 115
  • 163
  • 3
    Please identify and post the code that's raising this exception. It's not happening in a foreach block, but rather in a linq query. – phoog Mar 27 '12 at 17:46
  • 1
    Disagreed that this is a duplicate. The problem here is the result of a bug in MVC4. The underlying cause of the MVC4 bug is perhaps a duplicate of the linked question - but the answers provided by that question won't solve this problem. @RyanVersaw's answer (upgrading to MVC5) seems to apply more directly here. – Rob Hruska Dec 05 '13 at 19:24

2 Answers2

5

I've been seeing this issue pop up for us as well, so I did some digging. It's pretty clear this happens deep within MVC based on the stack trace, but it looks like MVC 5 should fix this. The version on the left is MVC4 and the one on the right is the current version of MVC5 at the time of posting this. If you check out the RemoveDuplicates() method, it looks like it's been modified to prevent exceptions like we're seeing.

Ryan Versaw
  • 6,417
  • 3
  • 30
  • 31
0

More code of your list instantiation would help.

Realistically, I would check that the enumerable that you pass to the list constructor is not changed. When you use IEnumerable, you are generating the enumerable at that point, whereas IQueryable allows you to defer the generation of the enumerable until a control point of execution.

To me, it looks like you are generating an IEnumerable To pass to the list constructor, but you are modifying the source list before you do.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129