【发布时间】:2017-01-26 03:06:16
【问题描述】:
我正在使用实体框架,并且我有一个列表,我需要遍历它才能完成工作。我无法直接通过数据库查询来完成这项工作,而且列表可能很大,所以我希望我可以使用 Parallel Foreach 或 AsParallel。
问题是,即使我使用 ToList() 将列表加载到内存中,然后在并行函数中运行它,它也会破坏我延迟加载的导航属性。
我正在运行这样简单的东西(为了这个问题,这已经被简化了很多):
var quoteList = DbContext.Quotes.ToList();
List<QuotesItem> quoteViewItemList = new List<QuotesItem>();
quoteList.ForAll(quote => {
var quoteViewItem = new QuotesItem();
quoteViewItem.YearEnding = quote.QuoteService.FirstOrDefault().Yearending; //is SOMETIMES null when it shouldn't be.
quoteViewItem.Manager quote.Client.Manager.Name; //IS sometimes null
quoteViewItem.... = ..... // More processing here
quoteViewItemList.Add(quoteViewItem);
});
问题是 QuoteService 有时似乎为空,即使它在列表中不为空。
【问题讨论】:
-
quoteList不是List<T>吗?如果是这样,它不会有一个名为QuoteService的成员。你的意思是quote.QuoteService.....? -
是的,这是一个错误。我现在正在纠正它。