CSharp - Range for PLINQ

Introduction

Range operator creates ParallelQuery<int> containing a sequence of incrementing integers.

This is a static method of the ParallelEnumerable class, rather than an extension method.

Prototypes

public static ParallelQuery<int> Range(
    int start,
    int count
)

The first parameter is the integer value that the sequence should begin with;

The second is the number of integer that should be in the sequence.

The Range method returns a ParallelQuery<int> that has incrementing values.

The following code shows the use of a parallel range.

We call the static Range method to create a ParallelQuery<int> that contains 10 integers, starting with the value 0.

We then enumerate all the items in the sequence using a foreach loop and print them out.

We use the same range sequence as the basis for a PLINQ query where we select the even integer values and print them out.

Demo

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

class Program//from w w  w.java  2s  . c  o  m
{
    static void Main(string[] args)
    {
        ParallelQuery<int> pq = ParallelEnumerable.Range(0, 10);
        
        foreach (int i in pq) {
            Console.WriteLine("Value {0}", i);
        }
        
        IEnumerable<int> results = from i in pq
                                   where i % 2 == 0
                                   select i;
        
        foreach (int i in results) {
            Console.WriteLine("Match: {0}", i);
        }
    }
}

Result