问题描述
我有一个包含大量项目的列表 - 最多 10,000 个.
我正在寻找从 IQueryable/List 中排除这些的最有效方法.
由于获取此 ID 列表所涉及的过程很复杂,因此无法在查询中执行此操作.
下面的示例具有极高的开销,想知道是否有人能够解释其可能的原因以及是否有更好的方法来实现这一点?
results = from q1 in results where excludedRecords.All(x => x != q1.ItemId) select q1;
推荐答案
这只是代码片段,但看起来您有两个列表 - results 和 excludeRecords.对于结果中的每个元素,您将遍历 excludeRecords 中的所有元素.这就是它慢的原因,它是 O(N x M)
Linq 和 sql 通过加入来解决这个问题,如果你加入(或等价的)你应该会看到一些不错的性能,因为这对我来说就像 O(NlgM)
看起来像这样(现在无法测试)
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;
问题描述
I have a List containing a high number of items - up to 10,000.
I am looking for the most efficient way to exclude these from an IQueryable/List.
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;
推荐答案
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 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;