简体   繁体   English

根据属性从列表中删除项目

[英]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: 我有一种奇怪的情况,代码行执行需要5分钟以上的时间,而我不明白为什么:

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. 我在这里尝试做的是从“ competitorTransactions”列表中删除“ endedItemsLocal”(IEnumerable)中包含的所有项目。

Why is it taking 5-9 minutes to execute ? 为什么要花5到9分钟才能执行? 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 ? 我可以肯定地说,如果这两个收藏集包含数百万条记录,则需要9分钟,但是只有921和8,这让我发疯了,我不明白为什么要花这么长时间? Can someone help me out ? 有人可以帮我吗 ? :/ :/

You should not work with IEnumerable since it is lazy loaded. 您不应使用IEnumerable,因为它是延迟加载的。 Use ToArray of endedItemsLocal and work with that collection, that will allocate a collection and thereby boost the speed. 使用endItemsLocal的ToArray并使用该集合,它将分配一个集合,从而提高速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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