C# Program to find the greatest number in an array using the WHERE clause LINQ

In this article, we will find the greatest number in an array using the WHERE clause in LINQ. LINQ (Language Integrated Query) provides a unified way to query data from different sources in C#, making code more readable and concise with built-in filtering, sorting, and grouping capabilities.

Language Integrated Query (LINQ)

LINQ is a component of the .NET framework that allows type-safe data access from various sources like databases, XML documents, and collections. It uses clauses to perform different operations on data

  • From Clause Specifies the data source and range variable.

  • Where Clause Filters values based on specified conditions.

  • Select Clause Projects and transforms each element into a new form.

  • Group Clause Groups data elements based on given conditions.

Required Namespaces

The following namespaces are essential for our implementation

  • System.Linq Provides LINQ functionality for querying data sources with methods like Where, Select, and OrderBy.

  • System.Collections.Generic Contains generic collections like List, Dictionary, and arrays for storing data.

Syntax

The basic syntax for using WHERE clause in LINQ

var result = from variable in dataSource where condition select variable;

Alternative method syntax

var result = dataSource.Where(condition);

Finding the Greatest Number Using WHERE Clause

Example 1: Numbers Greater Than a Specific Value

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] numbers = { 10, 200, 23, 50, 30, 40, 75, 120 };
      int threshold = 50;
      
      // Using LINQ WHERE clause to find numbers greater than threshold
      var greaterNumbers = from num in numbers 
                          where num > threshold 
                          select num;
      
      Console.WriteLine($"Numbers greater than {threshold}:");
      foreach (var number in greaterNumbers) {
         Console.Write(number + " ");
      }
      
      // Find the greatest among filtered numbers
      if (greaterNumbers.Any()) {
         int greatest = greaterNumbers.Max();
         Console.WriteLine($"\nGreatest number: {greatest}");
      }
   }
}

The output of the above code is

Numbers greater than 50:
200 75 120 
Greatest number: 200

Example 2: Using Method Syntax

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] numbers = { 45, 67, 23, 89, 12, 56, 78, 34 };
      int minValue = 40;
      
      // Using method syntax with Where clause
      var filteredNumbers = numbers.Where(x => x > minValue);
      
      Console.WriteLine($"Numbers greater than {minValue}:");
      Console.WriteLine(string.Join(", ", filteredNumbers));
      
      // Find greatest using Max()
      int greatest = filteredNumbers.Max();
      Console.WriteLine($"Greatest number: {greatest}");
   }
}

The output of the above code is

Numbers greater than 40:
45, 67, 89, 56, 78
Greatest number: 89

Example 3: Multiple Conditions with WHERE

using System;
using System.Linq;

class Program {
   static void Main() {
      int[] numbers = { 15, 42, 67, 23, 89, 12, 56, 78, 34, 91 };
      
      // Find greatest even number greater than 20
      var evenGreaterThan20 = from num in numbers 
                             where num > 20 && num % 2 == 0 
                             select num;
      
      Console.WriteLine("Even numbers greater than 20:");
      Console.WriteLine(string.Join(", ", evenGreaterThan20));
      
      if (evenGreaterThan20.Any()) {
         int greatestEven = evenGreaterThan20.Max();
         Console.WriteLine($"Greatest even number > 20: {greatestEven}");
      }
   }
}

The output of the above code is

Even numbers greater than 20:
42, 56, 78, 34
Greatest even number > 20: 78

LINQ WHERE Clause Process Array [10,200,23,50,30] WHERE num > 30 Filtered [200,50] Max() = 200 Greatest Number

Time Complexity

The time complexity of using WHERE clause with LINQ is O(n), where n is the size of the array. The WHERE clause iterates through each element once to apply the filter condition, and the Max() operation also requires O(n) time in the worst case.

Conclusion

The WHERE clause in LINQ provides an elegant way to filter arrays and find specific elements like the greatest number based on conditions. Combined with methods like Max(), it offers a readable and efficient approach to data querying in C#.

Updated on: 2026-03-17T07:04:36+05:30

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements