C# Program to Find Greatest Numbers in an Array using WHERE Clause LINQ
Last Updated :
18 Oct, 2021
Improve
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 will learn how to find the greatest numbers in an array using WHERE Clause LINQ. Here, we will get the numbers that are greater than a particular number in the given array.
Example:
Input: Array of Integers: 100,200,300,450,324,56,77,890 Value: 500 Output: Numbers greater than 500 are: 890 Input: Array of Integers: 34,56,78,100,200,300,450,324,56,77,890 Value: 100 Output: Numbers greater than 100 are: 200,300,450,324,890
Approach:
To display the greatest numbers in an array using WHERE Clause LINQ follow the following approach:
- Store integer(input) in an array.
- The sum of the elements is calculated using the for loop.
- The numbers which are greater than particular value is checked using where function.
- By using the LINQ query we will store the numbers in an iterator.
- Now the iterator is iterated and the integers are printed.
Example:
C#
// C# program to print the greatest numbers in an array // using WHERE Clause LINQ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class GFG{ static void Main() { // Array of numbers int [] array1 = { 34, 56, 78, 100, 200, 300, 450, 324, 56, 77, 890 }; // Now get the numbers greater than 100 and // store in big variable using where clause var big = from value in array1 where value > 100 select value; Console.WriteLine( "Numbers that are greater than 100 are :" ); // Get the greater numbers foreach ( var s in big) { Console.Write(s.ToString() + " " ); } Console.Read(); } } |
Output:
Numbers that are greater than 100 are : 200 300 450 324 890