c# - 并行内连接

标签 c# linq plinq parallel-extensions

我想使用 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()在第二个序列上也是如此。

来自MSDN documentationJoin 上重载,其中第二个序列的类型为 IEnumerable<TInner> :

This Join overload should never be called. This method is marked as obsolete and always throws NotSupportedException when invoked.

还要注意声明中的 obsolete-attribute:

[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
)

关于c# - 并行内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15008415/

相关文章:

c# - 如何在 datarepeater 中更改样式行选择

c# - C# 中的日期时间分析器错误

c# - 使用 linq 更新列表中的列表

.net - .NET 4.0和4.5中损坏的PLINQ ForAll

c# - 带有 IQueryable 扩展方法的最小起订量

c# - YouTube HTML Agility Pack C#

c# - 字符串或二进制数据将在 Linq 中被截断

c# - 从平面表中表示的父/子关系创建树

c# - PLINQ 查询给出溢出异常

c# - AsParallel() 如何拆分它的 'source' ?