Open In App

C# Program to Generate Numbers that are Multiples of 5 Using the LINQ Parallel Query

Last Updated : 01 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this article, we are going to generate the numbers from the given range which are multiples of 5 parallelly. So to do our task we are using the inbuilt ParallelQuery class to generate numbers in parallel.

Syntax:

((ParallelQuery<int>)ParallelEnumerable.Range(start, stop))

Where start is the starting number and stop is the ending number.

Example:

Input  : Range(3, 20)
Output :
10
15
20
5

Input  : Range(1,10)
Output :
5
10

Example:

In the below example, first, we will create a number collection of IEnumerable types range from 3 to 20 then generate numbers that are multiples of 5 present in between the given range using Where(n => n% 5 == 0) function. After generating numbers that are multiples of 5 we will display these numbers on the output screen. 

C#




// C# program to generate numbers that 
// are multiples of 5 
using System;
using System.Collections.Generic;
using System.Linq;
  
class GFG{
      
static void Main(string[] args)
{
      
    // Input numbers from 3 to 20
    // Using ParallelQuery
    IEnumerable<int> result = ((ParallelQuery<int>)ParallelEnumerable.Range(3, 20))
      
    // Generate numbers that are multiples 
    // of 5 in parallel
    .Where(n => n % 5 == 0).Select(res => res);
  
    // Display the numbers which are multiples of 5
    Console.WriteLine("Numbers are:");
    foreach (int numbers in result) 
    {
        Console.WriteLine(numbers); 
    }
}
}


Output:

Numbers are:
10
15
20
5

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads