"Collection was modified; enumeration operation may not execute" error when executing a member list
- 2 years ago
There are three issues with this code example
a) You are performing an unnecessary GetMemberId inside the loop. Instead you can just refer to FlowMemberId = FlowMember.MemberID. Try to limit the references to member names as much as possible, especially inside loops.b) You are trying to append a member to the same list that you are iterating through. That is why the error message says the collection [that you are looping through] is being modified. You should instead add the FlowMember to a NEW (initially empty) list.
c) You are trying to append a member ID (which is a number) to a list of member objects (not list of MemberIDs).
This modified snippet should work better (although I have not tested it for real):Dim objMemberListHeader As New MemberListHeader(args.MemberListArgs.MemberListName) Dim sourceFlowList As List(Of Member) = api.Members.GetBaseMembers(api.Pov.FlowDim.DimPk, api.Members.GetMemberId(api.Pov.Flow.DimTypeID,"TF")) Dim resultFlowList As New List(Of Member) For Each FlowMember In sourceFlowList If api.Flow.SwitchType(FlowMember.MemberId) Then resultFlowList.Add(FlowMember) Next Dim objMemberList As New MemberList(objMemberListHeader, resultFlowList) Return objMemberList
a more compact version could be done by using Linq , like this. (for example purposes only, this is not tested for real):
Dim objMemberListHeader As New MemberListHeader(args.MemberListArgs.MemberListName) Dim sourceFlowList As List(Of Member) = api.Members.GetBaseMembers(api.Pov.FlowDim.DimPk, api.Members.GetMemberId(api.Pov.Flow.DimTypeID,"TF")) Dim resultFlowList As List(Of member) = sourceFlowList.Where( Function(thisFlow) (api.Flow.SwitchType(thisFlow.MemberId)) ) Dim objMemberList As New MemberList(objMemberListHeader, resultFlowList) Return objMemberList