C# - Generate numbers that are multiples of 5 using LINQ Parallel Query

Learn, how to generate numbers that are multiples of 5 using the LINQ parallel query in C#? By Nidhi Last updated : April 01, 2023

Generating numbers multiples of 5

In this program, we will generate random numbers that are multiples of 5 using LINQ parallel query, here we will put a condition to check number is multiple of 5 or not in the specified range. Here we used range 1 to 20.

C# program to generate numbers that are multiples of 5 using the LINQ parallel query

The source code to generate numbers that are multiples of 5 using the LINQ parallel query in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to generate numbers that are 
//multiples of 5 using LINQ parallel query.

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

class Program {
  static void Main(string[] args) {
    IEnumerable < int > Numbers = ((ParallelQuery < int > ) ParallelEnumerable.Range(1, 30))
      .Where(num => num % 5 == 0)
      .Select(val => val);

    foreach(int num in Numbers) {
      Console.WriteLine(num);
    }
  }
}

Output

5
10
20
25
15
30
Press any key to continue . . .

Explanation

In the above program, we created a Program class that contains the Main() method.

IEnumerable Numbers = ((ParallelQuery)ParallelEnumerable.Range(1,30))
.Where(num => num%5==0)
.Select(val => val);

Here we created integer numbers collection of IEnumerable type. Here we generated numbers that are multiples of 5. Then we printed the random numbers between the range 1 to 50 on the console screen.

C# LINQ Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.