如何从 EnumerableRowCollection中删除 DataRow?
我想从 EnumerableRowCollection 中删除数据行。这可能吗?我使用 EnumerableRowCollection 而不是通用 var 变量来理解我的上下文。
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??
}
I want to delete a datarow from EnumerableRowCollection. Is that possible?. I am using EnumerableRowCollection instead of generic var variable to make sense my context.
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??
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,您无法从使用
foreach
迭代的集合中删除元素。这是完全禁止的,并且会导致运行时异常。您可能只想将此逻辑移至
linq
查询中:这将返回包含您真正想要的元素的列表,而无需在
foreach
循环中删除这些元素。For sure you cannot remove elements from collection you are iterating using
foreach
. That's simply forbidden and will result in runtime exception.You might simply want to move this logic into
linq
query:This will return to you list with elements that you really want, with no need to remove those elements in
foreach
loop.