CSharp - PLINQ ForAll method

Introduction

Used on a ParallelQuery, ForAll performs a System.Action on each item in the sequence.

We can output element using the ForAll method.

We use the Where method to filter the sequence and we print out the names directly using a lambda expression passed to the ForAll method.

Demo

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

class Program/*w w  w  . jav a  2s. c om*/
{
    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 Topic