c# - Collection was modified; enumeration operation might not execute on Except operation -


i trying remove rows datatable dt in loop above exception:

while (dt.rows.count > 0 && retry < globals.pushretrylimit) {     var query = dt.asenumerable().except(successbatch.asenumerable(), datarowcomparer.default)             .asenumerable().except(failbatch.asenumerable(), datarowcomparer.default);     if (dt.asenumerable().any())         dt = query.copytodatatable(); } 

successbatch , failbatch both datatable clones of dt.

in other questions error has been asked, dealing foreach loop. why error occur?

stacktrace:

   @ system.data.datatableextensions.loadtablefromenumerable[t](ienumerable`1 source, datatable table, nullable`1 options, fillerroreventhandler errorhandler)    @ system.data.datatableextensions.copytodatatable[t](ienumerable`1 source) 

you changing elements in collection (your datatable) while looping on foreach.

foreach queries enumerator , asks next element. if remove item enumerator's state becomes invalid. enumerator has store date indicating current position.

you should not this. maybe use collection keep track of changes or use concurrent collections (read classes in link)


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -