ThinkGeo.com    |     Blog    |     Wiki    |     Support

Collection was modified; enumeration operation may not execute issue while executing FeatureSource.GetFeaturesByColumnValue method

Hi,
I am getting the ‘Collection was modified; enumeration operation may not execute’ exception while executing FeatureSource.GetFeaturesByColumnValue method.
The exception not getting every time, Application having timer to refresh updated data on the map. Some rare cases only It’s happening. I am not applying DrawingExceptionMode property to Layer/Overlay.

Code:
ftsActualRoute = imActualRouteLayer.FeatureSource.GetFeaturesByColumnValue(“FlightID”, _sFlightID);

Can you please give me some suggestions to resolve this issue.

Reagrds,
Riyaz

Hi Riyaz,

If it’s a shape file feature layer, please make sure you open the layer by ReadOnly mode.

ShapeFileFeatureLayer imActualRouteLayer = new ShapeFileFeatureLayer(sourceShapeFile, ShapeFileReadWriteMode.ReadOnly);

Because your website will be access by multiply user at same time, if you modify the features when you try to query it, this exception will be thrown, that’s because it contains foreach logic.

So if it’s an inMemoryFeatureLayer, and you have to modify its feature, please lock it when you modify it, or you can copy the layer, then query in the copied one.

Wish that’s helpful.

Regards,

Ethan

Hi Ethan,
Thanks for your valuable information.
One more query, If we get features from InMemoryFeatureLayer by using LINQ query like this.

Code:
fts = imAircraftLayer.InternalFeatures.AsEnumerable().Where(ft=>ft.ColumnValues[“FlightID”].ToString()== hsdSession_Get.FlightFollowingFlightID).ToList();

instead of using FeatureSource.GetFeaturesByColumnValue, then collection enumeration issue will not arise right?

Thanks,
Riyaz

Hi Riyaz,

In fact I don’t have more experience on use Linq, so I did a query about it, from this topic: https://stackoverflow.com/questions/23393805/collection-was-modified-exception-while-enumerating-in-new-subcollection

It looks to list then query is helpful, you can try it and see whether it get solved.

Regards,

Ethan