Home > Backend Development > C#.Net Tutorial > C# program to find maximum number in array using WHERE clause LINQ

C# program to find maximum number in array using WHERE clause LINQ

王林
Release: 2023-09-08 19:25:06
forward
1238 people have browsed it

C# 程序使用 WHERE 子句 LINQ 查找数组中的最大数字

introduce

In this article, we will find the maximum 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 methods to access data from different sources such as databases and XML documents. With LINQ, users can write more readable code, and the code is more concise and beautiful. It also provides other features such as filtering, sorting, grouping data, and even changing data. Before we continue, we will take a closer look at Language Integrated Query (aka LINQ) and we will also look at the different clauses under LINQ and namespaces, especially the ones we will use in our code.

Language integrated query

LINQ is a component of the .NET Framework that helps users access it in a type-safe manner. The best part of LINQ is that it provides a unified source of methods to access data from different sources such as databases and XML documents. With LINQ, users can write more readable code, and the code is more concise and beautiful. Among them, clauses are unique parts of query expressions on which different types of operations are performed. There are different types of clauses under LINQ that perform unique functions. Some

explained below
  • From clause It specifies the data source and also indicates the range value of any number.

  • Where clause It filters any value based on certain conditions.

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

  • Group Clause It groups data elements based on given conditions.

Namespaces

These are important parts of any C# code. It acts as the backbone or foundation of any C# code or even code in other languages. It is a way of grouping related classes, methods, files, functions, structures, and enumerations together. It's also a way to organize your code. Here we'll take a closer look at some namespaces and see practical ways to use them in our code.

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

  • System.Collections.Generic This namespace provides various data structures for manipulating and storing data. It allows users to create strongly typed collections for improved type safety. Under this namespace, some commonly used data structures include List, Dictionary, HashSet, Queue, Stack, and LinkedList.

  • System.Linq This namespace is mainly used to query data sources, such as stacks, arrays and queues. It allows us to write concise queries to access the database, similar to writing queries in SQL. Some commonly used query operations include Select, Where, Join, Any, Skip, OrderBy, Take, etc.

  • System.Threading.Tasks It is basically used for asynchronous programming. In simple terms we can say it is used for multiprogramming. Multiple tasks can be kept running in the background without affecting the main functionality of the code. Some commonly used types and classes are tasks, parallelism, cancellation tokens, etc.

algorithm

Step 1 − First, we need an array of integers that can be stored in an array. This array will be our input parameters and can be used to perform multiple operations. In addition to the array, we have a value, which will also be used as input. We need to print out all numbers greater than it.

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

Step 3 Now, use the where function to check for a number that is greater than the value variable.

Step 4 By using a LINQ query, we store all numbers greater than the variable value in an iterator.

Step 5 Now we will use the for each loop that can be used in C# language to iterate through the iterator.

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

Step 7 We have the required 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() + " ");
      }
   }
}
Copy after login

Output

The output is 
200 50 40 
Copy after login

time complexity

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

in conclusion

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

The above is the detailed content of C# program to find maximum number in array using WHERE clause LINQ. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template