I have below variables
public IEnumerable<SelectListItem> SelectedGroups { get; set; }
Dictionary<string,string> grpsList;
Below is my iteration
if(condition)
viemodel.SelectedGroups = selectlist1;
else
viemodel.SelectedGroups = selectlist2;
foreach (var item in viemodel.SelectedGroups) <<--Error message
grpsList.Remove(item.Value);
I am not modifying selectedgroups but using it as a reference to remove from grpsList but its failing in foreach declaration.
Added code,
selectlist1 contains reference to grpsList, something like this (removed additional joins and all other code)
from g in grpsList
select g).ToSelectList(g => g.Value, g => g.Key);
selectlist2 is different list.
Most likely
SelectedGroupspoint to a linq expression that involvesgrpsList.To avoid this problem do a
.ToList()before the assigment toSelectedGroups. (You can still keep the property signature asIEnumerable<SelectListItem>)Using your added code:
Note that if you do not do the
ToList()on a linq expression than every time you iterate the Enumerator the complete linq statement is evaluated. This can cause a lot of unnecessary performance issues.