我想从EnumerableRowCollection中删除一个数据行。这可能吗?我使用EnumerableRowCollection而不是泛型变量来解释我的上下文。
EnumerableRowCollection<DataRow> results = from myRow in myDataTable.AsEnumerable()
where results.Field("RowNo") == 1
select results;
foreach(DataRow result in results)
{
if(resultOk(result))
delete result from results??
}发布于 2011-12-22 04:11:48
确保不能从使用foreach迭代的集合中移除元素。这是被禁止的,并将导致运行时异常。
您可能只想将此逻辑转移到linq query中:
EnumerableRowCollection<DataRow> results = from myRow in myDataTable.AsEnumerable()
where myRow.Field("RowNo") == 1 && !resultOk(myRow)
select myRow; // note that you're returning myRow, not results, like in your code
// you need to pay attention to code samples you're providing to avoid misunderstanding这将返回到您真正想要的元素列表,而不需要在foreach循环中删除这些元素。
https://stackoverflow.com/questions/8595569
复制相似问题