CSharp - Filtering Data Without a Where Clause using ForAll method

Description

Filtering Data Without a Where Clause using ForAll method

Demo

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

class Program// w  w  w.  j  a va 2s. com
{
    static void Main(string[] args)
    {
        string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
        
        int count = 0;
        
        codeNames.AsParallel()
            .ForAll(p => {
                if (p.Contains('o')) {
                    System.Threading.Interlocked.Increment(ref count);
                }
            });
        
        Console.WriteLine("Matches: {0}", count);
    }
}

Result

The code above used the ForAll method to perform an action on every item in the data sequence.

We check to see whether the name contains the letter o and increment the counter if it does.

Action you specify in the ForAll method will be performed on partitions of your data sequence in parallel.

Related Topic