I've run into quite a few cases of Collection was modified; enumeration operation may not execute errors when returning the results of a LINQ query in a function, like this... (I should add the function acts as an implementation of an interface and the results leave this module to be used in another.)
Public Function GetTheFuzzyFuzzbuzzes() As IEnumerable(of FuzzBuzz) _
Implements IFoo.GetTheFuzzyFuzzBuzzes
Return mySecretDataSource.Where(Function(x) x.IsFuzzy)
End Function
Should I, as a rule, always call .ToArray when returning the result of LINQ query in a function or property getter if the underlying data has potential to be changed? I know there's a bit of an efficiency hit in doing this, but I have a feeling it's the safe thing to do, and thus should always be done to avoid temporal coupling issues.
Edit:
Let me do a better job explaining the problem domain.
We have a graph based implementation of our main area of concern, which is an optimization problem. Entities are represented as graph nodes. Edges weighted with various costs and other parameters express relationships between the nodes . As the user manipulates the data, we create different edges and evaluate the various options they can take against the current state to give them feedback on each option's results. Changes made to the data on the server by other users and programs are propagated immediately to the client via push technology. We use a lot of threading ...
... all this means that we have a lot of stuff happening in a very asynchronous manner.
Our program is split into modules (based on the single responsibility principle) with a contract project and a resolved-at-run-time implementation project, which means we rely heavily on interfaces. We typically pass data between the modules using IEnumerable (since they are kind-of-sort-of immutable).