CSharp - Parallel LINQ ForAll

Introduction

ForAll operator accepts an action that will be performed on each element in the source data sequence.

Prototypes

public static void ForAll<T>(
          this ParallelQuery<T> source,
          Action<T> action
      )

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

class Program/* w  ww .  j a va  2 s  . c o  m*/
{
    static void Main(string[] args)
    {
      string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};

      // Parallel LINQ query
      codeNames.AsParallel()
          .Where(p => p.Contains('o'))
          .ForAll(p => Console.WriteLine("Name: {0}", p));
    }
}

Result

Related Topics