我想使用PLINQ在产品和类别之间执行内部联接。但是我不确定是否应该为这两个集合调用AsParallel方法。

// PLINQ  - Option 1
jointTables =     from c in Homework02.categories.AsParallel()
                  join p in Homework02.productList on c.Name equals p.Category
                  select new { Category = c, Product = p };

// PLINQ  - Option 2
jointTables = from c in Homework02.categories.AsParallel()
              join p in Homework02.productList.AsParallel() on c.Name equals p.Category
              select new { Category = c, Product = p };

最佳答案

您应该使用选项2,即在第二个序列上也显式调用AsParallel()

Join重载的MSDN documentation开始,其中第二个序列的类型为IEnumerable<TInner>


  永远不要调用此Join重载。该方法标记为
  作废,在调用时始终抛出NotSupportedException。


另请注意声明中的过时属性:

[ObsoleteAttribute(@"The second data source of a binary operator
                    must be of type System.Linq.ParallelQuery<T>
                    rather than System.Collections.Generic.IEnumerable<T>.
                    To fix this problem, use the AsParallel() extension
                    method to convert the right data source to
                    System.Linq.ParallelQuery<T>.")]
public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult>(
    this ParallelQuery<TOuter> outer,
    IEnumerable<TInner> inner,
    Func<TOuter, TKey> outerKeySelector,
    Func<TInner, TKey> innerKeySelector,
    Func<TOuter, TInner, TResult> resultSelector
)

08-26 19:56