简体   繁体   中英

Removing item from a list based on property

I have an odd situation where the line of code takes more than 5 minutes to execute, and I cannot understand why:

var cleansedTransactions = competitorTransactions.Where(i => !endedItemsLocal.Contains(i.ItemID)).ToList();

To explain further:

competitorTransactions - is a list of transactions which in this case contains 921 record inside it;

endedItemsLocal - is an IEnumerable<string> - which in this case contains 8 records

What I'm trying to do here is to remove all items that are contained in "endedItemsLocal" (IEnumerable) from "competitorTransactions" list.

Why is it taking 5-9 minutes to execute ? I would be okay to say it takes 9 minutes if these two collections contained millions of records, but only 921 and 8, it's driving me crazy here, and I cannot understand why it takes this long ? Can someone help me out ? :/

You should not work with IEnumerable since it is lazy loaded. Use ToArray of endedItemsLocal and work with that collection, that will allocate a collection and thereby boost the speed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM