執行我有以下PLINQ查詢:進行AsParallel()順序地
// Let's get a few customers
List<Customer> customers = CustomerRepository.GetSomeCustomers();
// Let's get all of the items for all of these customers
List<CustomerItem> items = customers
.AsParallel()
.SelectMany(x => ItemRepository.GetItemsByCustomer(x))
.ToList();
我期望GetItemsByCustomer()
到並行執行鍼對每個客戶,但它依次運行。
我試圖迫使並行,但仍然沒有運氣:
List<CustomerItem> items = customers
.AsParallel()
.WithExecutionMode(ParallelExecutionMode.ForceParallelism)
.SelectMany(x => ItemRepository.GetItemsByCustomer(x))
.ToList();
方法簽名:
private IEnumerable<Item> GetItemsByCustomer(Customer customer)
{
// Get all items for a customer...
}
據this article,PLINQ當然可以採取連續路線,如果它認爲合適的,但強制並行仍然應該覆蓋這一點。
注:上面的實例是純說明性的 - 假定customers
爲小的列表,並GetItemsByCustomer
是一種昂貴的方法。
你能給出一個完整的,獨立的例子嗎? – nvoigt 2014-10-06 06:26:54
您的真實代碼是否相同,或者您正在使用'SelectMany'的超載索引作爲參數? – 2014-10-06 07:00:07
@SriramSakthivel:我的代碼在結構上與上面相同。 – davenewza 2014-10-06 07:05:31