执行我有以下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