User987
User987

Reputation: 3825

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 ? :/

Upvotes: 0

Views: 56

Answers (1)

Hamit G&#252;ndogdu
Hamit G&#252;ndogdu

Reputation: 204

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.

Upvotes: 2

Related Questions