C# Program to Find the Negative Double Numbers From the List of Objects Using LINQ


Introduction

In this article, we will learn how to write a C# program to find the negative double numbers from the list of objects using LINQ.

Let's have a small overview of the language. The C# programming language is frequently used to develop desktop, web, and mobile apps. Working with big volumes of data can be difficult in any programming language. Finding data points that satisfy a certain set of requirements or filtering out specific values is a frequent activity when working with data. The Linguistic Integrated Query (LINQ) feature of C# can be utilised to simplify and improve data handling. Developers may quickly query data from various sources, including arrays, collections, and databases. It gives developers access to a syntax that is comparable to SQL (Structured Query Language) and enables simple data manipulation and sorting. As LINQ's syntax is similar to SQL's, developers can learn and utilize it with ease.

Problem Statement

In this article, we will demonstrate how to find the negative double numbers from the list of objects using LINQ. To find the negative double numbers from this list using LINQ, we need to perform the following steps −

  • Filter out the double values from the list.

  • Filter out the negative double values from the list.

And we can achieve this by utilising the Where() function in conjunction with the OfType() method. Let's go over each of them individually along with their syntax −

OfType()

The OfType() method is used to filter an IEnumerable's elements according to a given type. Or, to put it another way, this method is used to filter the list or sequence source based on whether they have the capability to cast a collection of items to a particular type. If the supplied source is null, an ArgumentNullException will be thrown.

Syntax

public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult>(this System.Collections.IEnumerable source); 

Where()

The value is filtered using the Where() method according to the predicate function. You may also say that it returns the values from the sequence or list given in accordance with the specified conditions or requirements.

Syntax

Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 

Let us create a list of objects of different data types.

Input List: { 3.14, -2.71, "hello", 0, “-7.5”, "world", -4.2, ‘a’, -3}

We have got the list with different element of different data types. Now the output will only be consist of negative double numbers.

Output: {-2.71, -4.2}

Let us understand

Algorithm

Step 1 − Create a list of objects

List<object> list = new List<object>() 

Step 2 − Choose the double values from the list.

We can use LINQ's OfType() method to remove the double values from the list. Only the elements of the provided type “double” are returned by this function after filtering the collection. In our situation, we wish to remove the list's double values solely. Here is how we can apply this strategy −

var doubles = list.OfType<double>(); 

Step 3 − Out of the double value chosen from the list, pick the negative double values. Negative double values must be filtered from the collection using a filter. The LINQ Where() method can be used to accomplish this. Just the elements that match the filter are returned by this method after applying a filter to the collection. In this instance, we simply want to filter the collection's negative double values. Here is how we can apply this technique.

var negativeDoubles = doubles.Where(d => d < 0); 

Step 4 − At last, Using the foreach loop, print the negative double numbers.

foreach (var d in doubles)
{
   Console.WriteLine(d);
}

These are some simple steps to get the negative double numbers from the list. Let's have a look at the code.

Example

using System;
using System.Collections.Generic;
using System.Linq;
class Program {
   static void Main(string[] args) {
      List<object> list = new List<object> {
         -2,3.14, -2.71, "hello", 0,"-7.5" , "world", -4.2
      };
      var doubles = list.OfType<double>().Where(d => d < 0);
      foreach (var d in doubles) {
         Console.WriteLine(d);
      }
   }
}

Output

The output for the above code is −

-2.71 
-4.2 

Note − Here you might be thinking why there is no mention of -2 and -7.5. It is because -2 is a negative number but not a double number. And -7.5 is written as “-7.5” which is considered as a string.

Conclusion

In this post, we've shown you how to use LINQ in C# to find the negative double numbers from a list of objects. We have used two methods Where() and OfType() to write the code. We discussed the Where() and OfType() method briefly. The problem statement is explained with an example. After we have discussed the algorithm. And at the last, the code and output are displayed. We hope that this article helped you to enhance your knowledge and understanding of the topic.

Updated on: 31-Mar-2023

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements