“收集被修改;枚举操作可能不会执行“内部System.Data.TypedTableBase<>
我有一个.NET 4.0程序集;它在GAC中注册并作为BizTalk"业务流程"的一部分。
有时我收到以下错误 - "收藏品被修改;枚举操作可能无法执行。 :System.InvalidOperationException:集合已被修改;枚举操作可能不会执行。"我不能重现它;当我对相同数据运行相同的
处理时,我的程序集不会在这个地方生成错误。
当我调用'时,会发生错误。 Where(< condition>)。ToArray()'用于数据表对象:类别为System.Data.TypedTableBase的对象< MyDataRowClass>。
这是代码:
$
I have an .NET 4.0 assembly; it is registered in GAC and works as part of a BizTalk "orchestration".
Sometimes I get the following error - "Collection was modified; enumeration operation might not execute. : System.InvalidOperationException: Collection was modified; enumeration operation might not execute.". I cannot reproduce it; when I run the same
processing of the same data, my assembly does not generate the error in this place.
The error happens when I call ‘.Where(<condition>).ToArray()’ for a datatable object: an object of classd System.Data.TypedTableBase<MyDataRowClass>.
Here is the code:
int? setTypeGroupId;
...
return instances.WorkContributors.Where
(
c =>
!c.IsInterestedPartyNoNull()
&& c.InterestedPartyNo == publisherIpNo
&& c.SetTypeNo == 1
&& (c.RecordType == "SPU")
&& c.TypeCode == "E"
&& (!setTypeGroupId.HasValue ||
(setTypeGroupId.HasValue && c.SetTypeGroupID == setTypeGroupId))
)
.ToArray();
对象'实例'是一个数据集 - 我的类是从System生成的.Data.DataSet。
属性'instances.WorkContributors'是一个数据表:System.Data.TypedTableBase< MyDataRowClass>类的对象。
类MyDataRowClass 从System.Data.DataRow生成。
$
错误后的调用堆栈如下:
The object ‘instances’ is a dataset – my class produced from System.Data.DataSet.
The property ‘instances.WorkContributors’ is a datatable: an object of class System.Data.TypedTableBase<MyDataRowClass>.
The class MyDataRowClass is produced from System.Data.DataRow.
The call stack after the error was the following:
集合被修改;枚举操作可能无法执行。 :System.InvalidOperationException:集合已被修改;枚举操作可能无法执行。
&NBSP; at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
&NBSP; at System.Linq.Enumerable。< CastIterator> d__97`1.MoveNext()
&NBSP; at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
&NBSP; at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
&NBSP; at System.Linq.Enumerable.ToArray [TSource](IEnumerable`1 source)
&NBSP; at MyProduct.FileParser.Types.CWR.PWRType.GetPublishers(CWRWorkInstances instances,Nullable`1 setTypeGroupId)
&NBSP; at MyProduct.FileParser.Validation.Concreate.PwrTypeValidation.ValidatePublisherNumber()
&NBSP; at MyProduct.FileParser.Validation.Concreate.PwrTypeValidation.Validate()
&NBSP; at MyProduct.FileParser.Types.CWR.PWRType.StoreRecord(CWRWorkInstances workInstances,CWRWorkParsingContext context)
&NBSP; at MyProduct.FileParser.Groups.CWR.NWRGroup.StoreGroup(Int32 workBatchID,CWRFileCommonData commonData)
&NBSP; at MyProduct.FileParser.CWRParser.ProcessCWRFile(String fileName,Boolean wait,Boolean deleteFile,String sourceFileName)
$
我无法理解为什么会发生错误;以及为什么它只是偶尔发生而且不会再次发生在相同的处理数据上。
错误"收集被修改;枚举操作可能不会执行。"对我来说,这本身就很简单;但我不明白为什么它会发生在我的代码中。 如果代码如下,则该错误除外:
Collection was modified; enumeration operation might not execute. : System.InvalidOperationException: Collection was modified; enumeration operation might not execute.
at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__97`1.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at MyProduct.FileParser.Types.CWR.PWRType.GetPublishers(CWRWorkInstances instances, Nullable`1 setTypeGroupId)
at MyProduct.FileParser.Validation.Concreate.PwrTypeValidation.ValidatePublisherNumber()
at MyProduct.FileParser.Validation.Concreate.PwrTypeValidation.Validate()
at MyProduct.FileParser.Types.CWR.PWRType.StoreRecord(CWRWorkInstances workInstances, CWRWorkParsingContext context)
at MyProduct.FileParser.Groups.CWR.NWRGroup.StoreGroup(Int32 workBatchID, CWRFileCommonData commonData)
at MyProduct.FileParser.CWRParser.ProcessCWRFile(String fileName, Boolean wait, Boolean deleteFile, String sourceFileName)
I cannot understand why the error happens; and why it happens only sometimes and does not happen on the same processed data again.
The error "Collection was modified; enumeration operation might not execute." Itself is pretty straightforward for me; but I do not see why it happens in that my code. The error is excepted if a code like this:
foreach (DataRow currRow in _someDataTable.Rows)
{
if (/*deletion condition*/)
{
someDataTable.Rows.Remove(currRow);
}
}
但我上面的代码只想枚举系统。 Data.TypedTableBase< MyDataRowClass>并将结果转换为数组。
$
任何想法?
But my code above just wants to enumerate System.Data.TypedTableBase<MyDataRowClass> and convert the result into an array.
Any ideas?