Getting started with C# LanguageVerbatim StringsOperatorsExtension MethodsCollection InitializersString InterpolationC# 6.0 FeaturesConstructors and FinalizersKeywordsGenericsReflectionInheritanceNull-Coalescing OperatorUsing StatementString Escape SequencesException HandlingNull-conditional OperatorsBuilt-in TypesLambda expressionsAsync-AwaitPropertiesThreadingUsing DirectiveMethodsYield KeywordEventsLINQ QueriesCommon String OperationsExpression TreesOverload ResolutionString.Formatnameof OperatorUnsafe Code in .NETInitializing PropertiesBindingList<T>ILGeneratorObject initializersXML Documentation CommentsPreprocessor directivesDynamic typeAnonymous typesStructsTuplesEnumAccess ModifiersTask Parallel LibraryAttributesGuidSingleton ImplementationDelegatesNullable typesGarbage Collector in .NetNetworkingArraysEquality OperatorLock StatementAction FiltersXmlDocument and the System.Xml namespaceDateTime MethodsBackgroundWorkerPolymorphismStatic ClassesIndexerIDisposable interfaceAliases of built-in typesImmutabilityXDocument and the System.Xml.Linq namespaceC# 7.0 FeaturesPerforming HTTP requestsGenerating Random Numbers in C#LoopingNamed ArgumentsDiagnosticsInterfacesIEnumerableNaming ConventionsAn overview of c# collectionsChecked and UncheckedRecursionFunctional ProgrammingLiteralsCastingNullReferenceExceptionFunc delegatesLINQ to XMLHash FunctionsHandling FormatException when converting string to other typesCryptography (System.Security.Cryptography)INotifyPropertyChanged interfaceValue type vs Reference typeC# 4.0 FeaturesIQueryable interfaceTask Parallel Library (TPL) Dataflow ConstructsStreamRuntime CompileConditional StatementsInteroperabilityOverflowEquals and GetHashCodeType ConversionParallel LINQ (PLINQ)String ManipulationString ConcatenatePartial class and methodsStopwatchesRegex ParsingC# ScriptC# 3.0 FeaturesAsync/await, Backgroundworker, Task and Thread ExamplesTimersFunction with multiple return valuesBinary SerializationMaking a variable thread safeIComparableCode ContractsIteratorsAssemblyInfo.cs ExamplesFile and Stream I/OCode Contracts and AssertionsCachingC# 5.0 FeaturesImplementing Flyweight Design PatternStringBuilderImplementing Decorator Design PatternAccessing DatabasesT4 Code GenerationMicrosoft.Exchange.WebServices.NET Compiler Platform (Roslyn)Data AnnotationUsing SQLite in C#System.Management.AutomationFileSystemWatcherSystem.DirectoryServices.Protocols.LdapConnectionNamed and Optional ArgumentsComments and regionsC# Authentication handlerPointers & Unsafe CodePointersHow to use C# Structs to create a Union type (Similar to C Unions)BigIntegerDependency InjectionReactive Extensions (Rx)Creational Design PatternsCreating a Console Application using a Plain-Text Editor and the C# Compiler (csc.exe)Reading and writing .zip filesGeneric Lambda Query BuilderImport Google ContactsLambda ExpressionsCLSCompliantAttributeObservableCollection<T>Synchronization Context in Async-AwaitICloneableRead & Understand StacktracesLinq to ObjectsASP.NET IdentityAccess network shared folder with username and passwordAsynchronous SocketStructural Design PatternsO(n) Algorithm for circular rotation of an arrayCreating Own MessageBox in Windows Form ApplicationIncluding Font ResourcesObject Oriented Programming In C#Using json.netGetting Started: Json with C#Windows Communication Foundation

Parallel LINQ (PLINQ)

Other topics

Simple example

This example shows how PLINQ can be used to calculate the even numbers between 1 and 10,000 using multiple threads. Note that the resulting list will won't be ordered!

var sequence = Enumerable.Range(1, 10000);
var evenNumbers = sequence.AsParallel()
                          .Where(x => x % 2 == 0)
                          .ToList();

// evenNumbers = { 4, 26, 28, 30, ... }
// Order will vary with different runs

WithDegreeOfParallelism

The degree of parallelism is the maximum number of concurrently executing tasks that will be used to process the query.

var sequence = Enumerable.Range(1, 10000);
var evenNumbers = sequence.AsParallel()
                          .WithDegreeOfParallelism(4)
                          .Where(x => x % 2 == 0);

AsOrdered

This example shows how PLINQ can be used to calculate the even numbers between 1 and 10,000 using multiple threads. Order will be maintained in the resulting list, however keep in mind that AsOrdered may hurt performance for a large numbers of elements, so un-ordered processing is preferred when possible.

var sequence = Enumerable.Range(1, 10000);
var evenNumbers = sequence.AsParallel()
                          .AsOrdered()
                          .Where(x => x % 2 == 0)
                          .ToList();

// evenNumbers = { 2, 4, 6, 8, ..., 10000 }

AsUnordered

Ordered sequences may hurt performance when dealing with a large number of elements. To mitigate this, it's possible to call AsUnordered when the sequence order is no longer necessary.

var sequence = Enumerable.Range(1, 10000).Select(x => -1 * x); // -1, -2, ...
var evenNumbers = sequence.AsParallel()
                          .OrderBy(x => x)
                          .Take(5000)
                          .AsUnordered()
                          .Where(x => x % 2 == 0) // This line won't be affected by ordering
                          .ToList();

Syntax:

  • ParallelEnumerable.Aggregate(func)
  • ParallelEnumerable.Aggregate(seed, func)
  • ParallelEnumerable.Aggregate(seed, updateAccumulatorFunc, combineAccumulatorsFunc, resultSelector)
  • ParallelEnumerable.Aggregate(seedFactory, updateAccumulatorFunc, combineAccumulatorsFunc, resultSelector)
  • ParallelEnumerable.All(predicate)
  • ParallelEnumerable.Any()
  • ParallelEnumerable.Any(predicate)
  • ParallelEnumerable.AsEnumerable()
  • ParallelEnumerable.AsOrdered()
  • ParallelEnumerable.AsParallel()
  • ParallelEnumerable.AsSequential()
  • ParallelEnumerable.AsUnordered()
  • ParallelEnumerable.Average(selector)
  • ParallelEnumerable.Cast()
  • ParallelEnumerable.Concat(second)
  • ParallelEnumerable.Contains(value)
  • ParallelEnumerable.Contains(value, comparer)
  • ParallelEnumerable.Count()
  • ParallelEnumerable.Count(predicate)
  • ParallelEnumerable.DefaultIfEmpty()
  • ParallelEnumerable.DefaultIfEmpty(defaultValue)
  • ParallelEnumerable.Distinct()
  • ParallelEnumerable.Distinct(comparer)
  • ParallelEnumerable.ElementAt(index)
  • ParallelEnumerable.ElementAtOrDefault(index)
  • ParallelEnumerable.Empty()
  • ParallelEnumerable.Except(second)
  • ParallelEnumerable.Except(second, comparer)
  • ParallelEnumerable.First()
  • ParallelEnumerable.First(predicate)
  • ParallelEnumerable.FirstOrDefault()
  • ParallelEnumerable.FirstOrDefault(predicate)
  • ParallelEnumerable.ForAll(action)
  • ParallelEnumerable.GroupBy(keySelector)
  • ParallelEnumerable.GroupBy(keySelector, comparer)
  • ParallelEnumerable.GroupBy(keySelector, elementSelector)
  • ParallelEnumerable.GroupBy(keySelector, elementSelector, comparer)
  • ParallelEnumerable.GroupBy(keySelector, resultSelector)
  • ParallelEnumerable.GroupBy(keySelector, resultSelector, comparer)
  • ParallelEnumerable.GroupBy(keySelector, elementSelector, ruleSelector)
  • ParallelEnumerable.GroupBy(keySelector, elementSelector, ruleSelector, comparer)
  • ParallelEnumerable.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector)
  • ParallelEnumerable.GroupJoin(inner, outerKeySelector, innerKeySelector, resultSelector, comparer)
  • ParallelEnumerable.Intersect(second)
  • ParallelEnumerable.Intersect(second, comparer)
  • ParallelEnumerable.Join(inner, outerKeySelector, innerKeySelector, resultSelector)
  • ParallelEnumerable.Join(inner, outerKeySelector, innerKeySelector, resultSelector, comparer)
  • ParallelEnumerable.Last()
  • ParallelEnumerable.Last(predicate)
  • ParallelEnumerable.LastOrDefault()
  • ParallelEnumerable.LastOrDefault(predicate)
  • ParallelEnumerable.LongCount()
  • ParallelEnumerable.LongCount(predicate)
  • ParallelEnumerable.Max()
  • ParallelEnumerable.Max(selector)
  • ParallelEnumerable.Min()
  • ParallelEnumerable.Min(selector)
  • ParallelEnumerable.OfType()
  • ParallelEnumerable.OrderBy(keySelector)
  • ParallelEnumerable.OrderBy(keySelector, comparer)
  • ParallelEnumerable.OrderByDescending(keySelector)
  • ParallelEnumerable.OrderByDescending(keySelector, comparer)
  • ParallelEnumerable.Range(start, count)
  • ParallelEnumerable.Repeat(element, count)
  • ParallelEnumerable.Reverse()
  • ParallelEnumerable.Select(selector)
  • ParallelEnumerable.SelectMany(selector)
  • ParallelEnumerable.SelectMany(collectionSelector, resultSelector)
  • ParallelEnumerable.SequenceEqual(second)
  • ParallelEnumerable.SequenceEqual(second, comparer)
  • ParallelEnumerable.Single()
  • ParallelEnumerable.Single(predicate)
  • ParallelEnumerable.SingleOrDefault()
  • ParallelEnumerable.SingleOrDefault(predicate)
  • ParallelEnumerable.Skip(count)
  • ParallelEnumerable.SkipWhile(predicate)
  • ParallelEnumerable.Sum()
  • ParallelEnumerable.Sum(selector)
  • ParallelEnumerable.Take(count)
  • ParallelEnumerable.TakeWhile(predicate)
  • ParallelEnumerable.ThenBy(keySelector)
  • ParallelEnumerable.ThenBy(keySelector, comparer)
  • ParallelEnumerable.ThenByDescending(keySelector)
  • ParallelEnumerable.ThenByDescending(keySelector, comparer)
  • ParallelEnumerable.ToArray()
  • ParallelEnumerable.ToDictionary(keySelector)
  • ParallelEnumerable.ToDictionary(keySelector, comparer)
  • ParallelEnumerable.ToDictionary(elementSelector)
  • ParallelEnumerable.ToDictionary(elementSelector, comparer)
  • ParallelEnumerable.ToList()
  • ParallelEnumerable.ToLookup(keySelector)
  • ParallelEnumerable.ToLookup(keySelector, comparer)
  • ParallelEnumerable.ToLookup(keySelector, elementSelector)
  • ParallelEnumerable.ToLookup(keySelector, elementSelector, comparer)
  • ParallelEnumerable.Union(second)
  • ParallelEnumerable.Union(second, comparer)
  • ParallelEnumerable.Where(predicate)
  • ParallelEnumerable.WithCancellation(cancellationToken)
  • ParallelEnumerable.WithDegreeOfParallelism(degreeOfParallelism)
  • ParallelEnumerable.WithExecutionMode(executionMode)
  • ParallelEnumerable.WithMergeOptions(mergeOptions)
  • ParallelEnumerable.Zip(second, resultSelector)

Contributors

Topic Id: 3569

Example Ids: 12311,12312,12313,12314

This site is not affiliated with any of the contributors.