C# Program to estimate the size of the file using LINQ


Introduction

Let us try to understand the C# program to estimate the size of the file using LINQ. We will be understanding the LINQ (Language Integrated Query) and its methods and classes and how with their help, we will get the desired output.

Language Integrated Query aka LINQ is used to generate queries in C# language. Previously we had to use other relational languages like SQL and XML. It provides more power to the C# language or any other .NET language. The syntax used to query the database in LINQ is the same as that of querying for data stored in an array.

In this article, we will fully understand how the use of LINQ in C# has completely transformed it and how it has made it much easy for users to access their data using C# language only without relying on other languages like SQL.

We will start by understanding the LINQ and some of its methods which will be helpful for us in understanding the code. Then we will understand the code and its algorithm.

Language Integrated Query (LINQ)

A part of the.NET framework called LINQ makes it easier for users to retrieve data in a typesafe way.

The best feature of LINQ is that it offers a single approach for gaining access to data from many sources, including databases and XML documents. Users may write code that is a lot easier to comprehend, much more succinct, and much more aesthetically pleasing with the aid of LINQ. It also provides other functionalities like filtering, sorting, grouping data and even changing them.

It comes under the System.

There are generally two types of classes under this namespace. The first one is the Enumerable class which holds standard query operators to query IEnumerable<T>. The second and last one is the Queryable class holds standard query operators that operate on an object which executes IQueryable<T>.

There are multiple advantages of considering using LINQ over any other database language like SQL. Some are given below −

  • It is a simple, high-level language and it is well ordered than SQL.

  • With its help, we can access any other data sources like XML, entities, and objects.

  • It supports query expression, lambda expression, and extension methods.

  • In the absence of LINQ, a C# program had to contain methods and objects to access any data. That amount of code was too big and its readability was too low in comparison to what LINQ provides as an alternative. LINQ provides a much better code that can be reused. It can be reused as the same code in other programs. The LINQ code used has much better maintainability and it has few errors due to type safety.

  • Full type checking is done at compile time, on the other hand, errors can be detected at runtime.

As we move ahead we need to know about the methods which will be helpful in having a better understanding of the code and its algorithms.

  • GetFiles − It returns the name of the file from the directory which is being given as an input parameter.

  • Select  It is used to get a single value from a specified sequence.

  • FileInfo  It is a class that provides different types of methods and properties for copying, deleting, and performing many other operations.

  • Math.Round method  This method is used to calculate the round-off value. Here in our algorithm, the value to which it will be rounded off will be 1.

These methods and classes will be of utmost value while understanding the code and the algorithm. These lay down a foundation for getting our desired output.

Algorithm

The algorithm below will give a total understanding of the code to estimate the size of the file using LINQ. We will know the step-by-step methods to have a deeper understanding of the code.

Step 1 − We will have a directory path with the help of the GetFiles method. We will be storing the path in the form of a string.

Step 2  Now we will be selecting the file using File methods.

Step 3  We will now calculate the average of the file using the Average() function.

Step 4  Now we will calculate the round-off value to 1 decimal place using Math. round function.

Step 5  Finally, we will print the size of the file using the console write line function.

Step 6  Initially when coding we have to make sure that we don’t miss out on declaring the required namespace that is System.Linq. With the help of this, we are able to code LINQ classes and methods and generate the desired output.

Example

using System;
using System.Linq;
using System.IO;
class TutorialsPoint{
   static void Main(string[] args){
      string[] s= Directory.GetFiles("c:\A");
      
      // Here, we are getting the file from the directory or path.
      var avg= s.Select(file => new FileInfo(file).Length).Average();
      
      // Here we are getting the average size of the file.
      avg = Math.Round(avg / 10, 1);
      
      // Now we are rounding off our value to 1 decimal point
      
      // We are storing the value in a variable named avg
      Console.WriteLine("The size of the file is:",avg);
      
      //Finally we will display the average file size.
   }
} 

Output

The size of the file is 1 MB. 

Time Complexity

In the code mentioned above to calculate the size of the file using LINQ, we are getting multiple methods and classes which as a whole give us the desired output. The time complexity of all those methods is constant which in Big-O notation can be called O(1). Thus the time complexity of the whole code is O(1).

Conclusion

In this article, we have extensively discussed the way to calculate the size of the file using LINQ. Not directly jumping over to the code and its algorithm, we first discussed what is LINQ, how it works, what are its advantages, and why we need it. Then we moved towards the respective methods, the code, and the algorithms We also understood the time complexity of the code. We hope this article has been helpful in enhancing your knowledge about C#.

Updated on: 31-Mar-2023

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements