如何从 EnumerableRowCollection中删除 DataRow?

发布于 2024-12-22 06:41:59 字数 446 浏览 1 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2024-12-29 06:41:59

当然,您无法从使用 foreach 迭代的集合中删除元素。这是完全禁止的,并且会导致运行时异常。

您可能只想将此逻辑移至 linq 查询中:

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 循环中删除这些元素。

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:

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

This will return to you list with elements that you really want, with no need to remove those elements in foreach loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文