C# Program to Display the Greatest Numbers in an Array using WHERE Clause LINQ
Posted by Superadmin on August 14 2022 14:12:09

C# Program to Display the Greatest Numbers in an Array using WHERE Clause LINQ

 

This is a C# Program to display the greatest numbers in an array using where clause linq.

Problem Description

This C# Program Displays the Greatest numbers in an Array using WHERE Clause LINQ.

Problem Solution

Here the where clause is used in a query expression to specify which elements from the data source will be returned in the query expression

Program/Source Code

Here is source code of the C# Program to Display the Greatest numbers in an Array using WHERE Clause LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Display 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 Program
{
    static void Main()
    {
        int[] numbers = { 500, 344, 221, 4443, 229, 1008, 6000, 767, 256, 0 };
        var greaterNums =
            from num in numbers
            where num > 500
            select num;
        Console.WriteLine("Numbers Greater than 500 :");
        foreach (var s in greaterNums)
        {
            Console.Write(s.ToString() + " ");
        }
        Console.Read();
    }
}
Program Explanation

This C# program is used to print the greatest numbers in an array using WHERE clause LINQ. We have already defined the values of ‘array[]’ variable. The where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. The foreach() function is used to print the element values greater than 50.

 
Runtime Test Cases
 
Numbers Greater than 500 :
4443 1008 6000 767