从链接到 SQL 数据库的数据集中高效删除行

发布于 2024-12-10 07:07:36 字数 944 浏览 0 评论 0原文

大家好
我正在从数据集中删除行。我要删除的所有行的共同点是该列具有相同的值,因此 .FirstOrDefault(x => x.stock == key) 顺便说一下,它是一个 int 。

 public bool RemoveStock(string tickerName) {       
        bool couldBeRemoved = false;
        int key = this.getKeyFromtickerName(tickerName);
        stockDataSet.ListingRow found = 
            listingDataTable.FirstOrDefault(x => x.stock == key);

        while (found != null) {
            listingDataTable.RemoveListingRow(found);     
            found = listingDataTable.FirstOrDefault(x => x.stock == key);
        }

        listingTa.Update(listingDataTable);
        listingDataTable.AcceptChanges();
        return couldBeRemoved;
    }

编辑 时间花费在循环中。我假设函数 .FirstOrDefault 从数据集的开头开始,如果我没记错的话,我有大约 250 万行。 结束编辑

该功能可以工作,但速度慢得令人痛苦。删除 7000 行需要 10 - 15 分钟的时间。这一定是一个更好的方法,但是如何呢?

最好的问候
戈尔根

Hello everone
I'm removing rows from a dataset. All rows that I want to delete have in common that the column have the same value, hence the .FirstOrDefault(x => x.stock == key) which is an int by the way.

 public bool RemoveStock(string tickerName) {       
        bool couldBeRemoved = false;
        int key = this.getKeyFromtickerName(tickerName);
        stockDataSet.ListingRow found = 
            listingDataTable.FirstOrDefault(x => x.stock == key);

        while (found != null) {
            listingDataTable.RemoveListingRow(found);     
            found = listingDataTable.FirstOrDefault(x => x.stock == key);
        }

        listingTa.Update(listingDataTable);
        listingDataTable.AcceptChanges();
        return couldBeRemoved;
    }

edit The time is spent in the loop. I assume that the function .FirstOrDefault starts from the beginning of the dataset and I have around 2.5 milion rows, if I remember correctly. end edit

The function works, but painfully slow. It take an order of 10 - 15 minutes to remove 7000 rows. It has to be a better way but how?

Best regards
Gorgen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

贵在坚持 2024-12-17 07:07:36

您正在使用 while 循环从头开始迭代所有记录。您只需迭代一次即可找到需要删除的记录,然后可以在循环中删除它们,如下所示:

var itemsToBeRemoved = listingDataTable.Where(x=>x.stock == key).ToList();
foreach (var item in itemsToBeRemoved) 
              listingDataTable.RemoveListingRow(item);

You are iterating all the records from the start with your while loop. You could just iterate it once to find the records that need to be removed and then you can remove them in a loop like so:

var itemsToBeRemoved = listingDataTable.Where(x=>x.stock == key).ToList();
foreach (var item in itemsToBeRemoved) 
              listingDataTable.RemoveListingRow(item);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文