开发者

efficient remove of row from dataset linked to SQL database

开发者 https://www.devze.com 2023-04-13 06:04 出处:网络
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

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.getKeyFromtick开发者_如何转开发erName(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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号