从链接到 SQL 数据库的数据集中高效删除行
大家好
我正在从数据集中删除行。我要删除的所有行的共同点是该列具有相同的值,因此 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用 while 循环从头开始迭代所有记录。您只需迭代一次即可找到需要删除的记录,然后可以在循环中删除它们,如下所示:
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: