This method takes in a list of massings, and attempts to group them according to their X and Y attributes. I attempted to do this by creating a dictionary Dictionary<int, List<Massing>>
called buildingList
where I add Massing
objects to List<Massing>
if the current Massing
object in the iteration has the same X and Y attributes as a Massing
object already in buildingList
, If they are not, I create a new dictionary entry with the accumulated counter
.
The error i am running into is Collection was modified, enumeration may not execute
. From my shallow understanding, I think this problem is occurring because I am trying to alter the value list by adding more items to the value list during run time.
I am not sure if this is the only issue or if the bug runs deeper but any guidance would be greatly appreciated. Also if there is an entirely better (AND PLEASE SIMPLER) way to do this, like with Linq, and some cool GroupBy operation, or any other way that would be interesting to see.
public Dictionary<int, List<Massing>> GroupMassing(List<Massing> massings)
{
// create dictionary {key = id : value = List<Breps>}
Dictionary<int, List<Massing>> buildingList = new Dictionary<int, List<Massing>>();
// init counter to be used as key
int counter = 1;
for (int i = 0; i < massings.Count; i++)
{
// Get center points and x and y
Massing myMassing = massings[i];
Point3d cPnt = myMassing.CPnt;
double xC = cPnt.X;
double yC = cPnt.Y;
// check if dic is empty
if (buildingList.Count == 0)
{
List<Massing> myMassings = new List<Massing>() { myMassing };
buildingList[counter] = myMassings;
}
// check the center point of current massing to all the center points of all the breps in the dictionary
else
{
foreach (KeyValuePair<int, List<Massing>> entry in buildingList)
{
List<Massing> list = entry.Value.ToList();
for (int j = 0; j < list.Count; j++)
{
Massing massing = list[j];
Point3d cPnt2 = massing.CPnt;
double xC2 = cPnt2.X;
double yC2 = cPnt2.Y;
if (xC2 == xC && yC2 == yC)
{
List<Massing> massingCopy = buildingList[counter];
massingCopy.Add(massing);
}
else
{
counter++;
List<Massing> myMassings2 = new List<Massing>() { myMassing };
buildingList[counter] = myMassings2;
}
}
}
}
}
return buildingList;
}