我正在尝试更新标准 LINQ 查询以使用 PLINQ。标准查询如下所示:
var query = (from doc in this.ownershipRepository.GetDocuments()
let queryStateCode = this.currentQuery.StateCode
let queryCountyCode = this.currentQuery.CountyCode
where queryStateCode != null ? doc.StateCode == queryStateCode : true &&
queryCountyCode != null ? doc.CountyCode == queryCountyCode : true
orderby doc.Owner
select doc)
.Take(QUERY_RESULTS_SIZE);
存储库中的 .GetDocuments() 方法很简单:
public class OwnershipRepository : IDisposable
{
private OwnershipEntities context;
...
public IQueryable<Document> GetDocuments()
{
return this.context.Documents;
}
...
}
这很好用。我尝试使用 PLINQ 来利用查询,如下所示:
var query = (from doc in this.ownershipRepository.GetDocuments().AsParallel()
let queryStateCode = this.currentQuery.StateCode
let queryCountyCode = this.currentQuery.CountyCode
where queryStateCode != null ? doc.StateCode == queryStateCode : true &&
queryCountyCode != null ? doc.CountyCode == queryCountyCode : true
orderby doc.Owner
select doc)
.AsSequential()
.Take(QUERY_RESULTS_SIZE);
但这会引发 System.OutOfMemoryException。我正在使用以下规格的机器上测试它:
Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz 2.79 GHz / 4 GB 内存
我需要做些什么来优化此查询,以免导致内存不足问题吗?