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


Introduction

In this article, we are going to find the greatest number in an array using the WHERE clause in LINQ. LINQ (Language Integrated query) is used to generate queries in C# Language. The best part of LINQ is that it provides a unified source of the method to access data from different sources like databases and XML documents. With the help of LINQ, users can write code that has much better readability and the code is much more concise and good-looking. It also provides other functionalities like filtering, sorting, grouping data and even changing them. Before moving ahead we are going to understand the Language Integrated query aka LINQ in detail and we will also be understanding the different clauses which come under LINQ and namespaces, especially those which we will be using in our code.

Language Integrated Query

LINQ is a component of.NET framework that helps users to access it in a type-safe manner. The best part of LINQ is that it provides a unified source of the method to access data from different sources like databases and XML documents. With the help of LINQ, users can write code that has much better readability and the code is much more concise and good-looking. In it, a clause is a distinct part of a query expression that performs different types of operations on it. There are different types of clauses under LINQ that perform unique functions. Some are explained below

  • From Clause  It specifies the data source and it also indicates the range value of any number.

  • Where Clause  It filters any value based on some condition.

  • Select Clause  It projects each element in a data source and transforms it into a new form.

  • Group Clause  it groups the data element based on the condition given.

Namespaces

These are an integral part of any C# code. It acts as a backbone or foundation of any C# code or even any other language code. It is a way to group related classes, methods, files, functions, structures, and enumerations together. It is also a way of organizing one’s code. Here we are going to study in deep about certain namespaces and we will see a practical way to use them in our code.

  • System.Text Namespaces  It is used to perform character and string encoding operations. It contains various functions which can be used to manipulate strings and work with regular expressions. For eg- Encoding, StringBuilder, Decoder, and Encoder are some of the methods used under this namespace.

  • System.Collections.Generic  This namespace provides various data structures used to manipulate and store data. It allows users to create strongly typed collections for better type safety. Some of the commonly used data structures under this namespaces are List, Dictionary, HashSet, Queue, Stack, and LinkedList are some of the commonly used data structures under this namespace.

  • System.Linq  This namespace is basically for querying data sources like Stack, Array, and queue. It allows one to write a concise query to access databases similar to how it is written in SQL. Some commonly used queries which we keep on seeing often are Select, Where, Join, Any, Skip, OrderBy, Take, etc.

  • System.Threading.Tasks  It is basically for asynchronous programming. In easy words, we can say that it is used for multiprogramming. Multiple tasks can be kept running in the background without affecting the main function of the code. Some commonly used types and classes are task, parallel, cancellation token, etc.

Algorithm

Step 1 − At first we need to have an array of integers that we can store in an array. This array will be our input parameter and it can be used to perform multiple operations. Apart from an array we also have a value that will also be input. We have to print all the numbers which are bigger than it.

Step 2  Now with the help of FOR loop, we can calculate the sum of the elements. We are storing the sum in a variable.

Step 3  Now, the numbers which are bigger than the value variable is checked using the where function.

Step 4  By using LINQ query we are storing all the numbers which are greater than the value variable in an iterator.

Step 5  Now we will iterate over the iterator using the for each loop which we can use in C# language.

Step 6  Finally, as we iterate over using the for each loop, we print the elements in order.

Step 7  We have the desired output in our output console.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//these namespaces are declared in advance to make sure the related function work in order.
class Tutorials_point{

   //declare class first.
   static void Main() {

      int[] arr = { 10,200,23, 50, 30,40};
      
      //Here, we are having a set of numbers stored in an array as input.
      
      // below we also have a value above which are the numbers will be printed
      var answer = from value in arr where value > 30 select value;
      
      //All the numbers which are greater than 100 are stored.
      Console.WriteLine("The output is");
      
      // Here, we are printing the desired output.
      foreach (var i in answer) {
         //we are iterating over using for each loop and printing all the numbers.
         
         // The numbers printed are our desired output.
         Console.Write(i.ToString() + " ");
      }
   }
}

Output

The output is 
200 50 40 

Time Complexity

In the code described above, we can see that we are iterating over the array using the foreach loop. Thus, we can deduce the fact that the time complexity of the code in the worst case can be the size of an array. So we can say that the time complexity of the above algorithm is O(size of the array).

Conclusion

In this article we have extensively talked about LINQ and namespaces which we used in our code so that one can understand the code better. For better understanding, we have also discussed the algorithm, the code, and its time complexity. We hope this article has been helpful in enhancing your knowledge about C#.

Updated on: 21-Apr-2023

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements