排除大型List< int>从LINQ到Entities查询 [英] Excluding large List<int> from LINQ to Entities query
问题描述
我有一个包含大量项目的列表-最多10,000个.
I have a List containing a high number of items - up to 10,000.
我正在寻找最有效的方法来将它们从IQueryable/List中排除.
I am looking for the most efficient way to exclude these from an IQueryable/List.
由于获取此ID列表所涉及的过程很复杂,因此不可能在查询中执行此操作.
Due to the complexity of the process involved in obtaining this list of Ids, it isn't possible to do this within a query.
下面的示例的开销非常大,想知道是否有人可以解释此问题的可能原因,以及是否有更好的方法来实现这一目标?
The example below has an extremely high overhead and wondered if anybody might be able to explain possible reasons for this and if there's a better way to achieve this?
results = from q1 in results
where excludedRecords.All(x => x != q1.ItemId)
select q1;
推荐答案
这只是代码的一部分,但是看起来您有两个列表-results和excludedRecords.对于结果中的每个元素,您都要遍历excludeRecordRecords中的所有元素.这就是为什么它很慢,它是O(N x M)
This is just a fragment of the code, but it looks like you have two lists - results and excludedRecords. For each element in results you iterate over all the elements in excludedRecords. This is why it is slow, it is O(N x M)
Linq和sql通过联接解决了这个问题,如果您联接(或等效联接),您应该会看到一些不错的性能,因为这会像O(NlgM)
Linq and sql solve this with joining, if you join (or the equivalent) you should see some nice performance since that will me something like O(NlgM)
看起来像这样(现在无法测试)
It would look something like this (can't test it right now)
var results2 = from q1 in results
join x in excludedRecords on q1.LeadID = x into joined
from z in joined.DefaultIfEmpty()
where z == null
select q1;
这篇关于排除大型List< int>从LINQ到Entities查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!