Open In App

C# Program to Print the Numbers Greater Than 786 in an Integer Array using LINQ

Improve
Improve
Like Article
Like
Save
Share
Report

Language-Integrated Query (LINQ) is a uniform query syntax in C# to retrieve data from different sources. It eliminates the mismatch between programming languages and databases and also provides a single querying interface for different types of data sources. In this article, we will learn how to display numbers greater than 786 in an array using LINQ.

Example: 

Input: 1, 234, 456, 678, 789, 987, 654, 345
Output: 789, 987 

Input: 3, 134, 856, 578, 789, 187, 854, 945
Output: 856, 854, 945

Approach: 

To print the numbers greater than 786 in an integer array we use the following approach: 

  • Store integer(input) in an array.
  • By using the LINQ query we will store the numbers greater than 786 in an iterator.
  • Now the iterator is iterated and the integers are printed.

Example:

C#




// C# program to display the numbers greater than 786 in
// an integer array using LINQ
using System;
using System.Linq;
 
class GFG{
     
static void Main()
{
     
    // Input
    int[] Arr = { 1, 234, 456, 678, 789, 987, 654, 345 };
     
    // From the array the numbers greater than 786 are
    // stored in to the iterator numbers.
    var numbers = from number in Arr where number > 786 select number;
     
    // Display the output
    Console.WriteLine("The numbers larger than 786 are :");
    foreach(int n in numbers)
    {
        Console.Write(n + " ");
    }
}
}


Output:

The numbers larger than 786 are :
789 987 

Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads