简体   繁体   English

C#PLINQ .AsParallel()在查询中的位置

[英]C# PLINQ .AsParallel() position in query

StackOverflow, 堆栈溢出,

Within C# PLINQ I understand the position of ".AsParallel()" impacts how the query is run. 在C#PLINQ中,我理解“ .AsParallel()”的位置会影响查询的运行方式。 For example, where ".AsParallel()" occurs in the middle of a query it will execute sequentially before the method and parallel after the method. 例如,当“ .AsParallel()”出现在查询中间时,它将在方法之前顺序执行,在方法之后并行执行。 ( PLINQ: Parallel Queries in .NET ). PLINQ:.NET中的并行查询 )。

My question is, with a more complex query (below), where ".AsParallel" occurs at the start of the query (as a prefix to .Select) will all following methods execute parallel also? 我的问题是,使用更复杂的查询(如下),其中“ .AsParallel”出现在查询的开头(作为.Select的前缀),以下所有方法也会并行执行吗? (currently, ".AsParallel" occurs after the .Select). (当前,“。AsParallel”出现在.Select之后)。

  Collection =
   typeof (Detail).GetProperties(BindingFlags.Public | BindingFlags.Instance)
     .SelectMany(propertyInfo => recentPhases
      .Where(phase => phase.Finalised)
      .SelectMany(phase => phase.PhaseDetail
      .Select(keyValuePair => new
     {
      phase.Direction,
      phase.Momentum,
      keyValuePair.Key,
      keyValuePair.Value
     }))
     .Select(arg => new
     {
      Key = new BmkKey
      {
       Direction = (arg.Direction == Dir.Up ? Dir.Up : Dir.Down),
       Momentum = (arg.Momentum == Mom.Price ? Mom.Price : Mom.Time),
       BarNumber = arg.Key,
       DetailType = propertyInfo.Name
      },
      Value = (double) propertyInfo.GetValue(arg.Value, null)
     }))
    .AsParallel().GroupBy(grp => grp.Key)
    .ToDictionary(grp => grp.Key, grp => new Distribution(grp.Select(x => x.Value)));

Yes everything will be executed parallel after the AsParallel() method is called. 是的,在AsParallel()方法之后,所有操作都将并行执行。 From msdn: 来自msdn:

public static ParallelQuery<TSource> AsParallel<TSource>(
this IEnumerable<TSource> source)

So the input is an IEnumerable<T> and the output a ParallelQuery<T> . 因此,输入为IEnumerable<T> ,输出为ParallelQuery<T>

If we then look at the ParallelEnumerable class : 如果我们再看一下ParallelEnumerable类

Provides a set of methods for querying objects that implement ParallelQuery{TSource}. 提供一组用于查询实现ParallelQuery {TSource}的对象的方法。 This is the parallel equivalent of Enumerable. 这与Enumerable并行。

So from then on, you won't be calling the methods defined for IEnumerable<T> but you will be calling their parallel counterparts defined for ParallelEnumerable . 因此,从那时起,您将不再调用为IEnumerable<T>定义的方法,而是将调用其为ParallelEnumerable定义的ParallelEnumerable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM