Home > Backend Development > C#.Net Tutorial > C# program to find negative double numbers from a list of objects using LINQ

C# program to find negative double numbers from a list of objects using LINQ

PHPz
Release: 2023-09-03 16:37:02
forward
1250 people have browsed it

使用 LINQ 从对象列表中查找负双精度数的 C# 程序

introduce

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

Let's give a brief overview of the language. The C# programming language is frequently used to develop desktop, web, and mobile applications. Processing large amounts of data can be difficult using any programming language. Finding data points that meet a specific set of requirements or filtering out specific values ​​are common activities when working with data. C#'s Language Integrated Query (LINQ) feature can be used to simplify and improve data processing. Developers can quickly query data from a variety of sources, including arrays, collections, and databases. It enables developers to use syntax equivalent to SQL (Structured Query Language) and supports simple data manipulation and sorting. Since LINQ's syntax is similar to SQL, developers can easily learn and use it.

Problem Statement

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

  • Filter out duplicate values ​​from the list.

  • Filter out negative double values ​​from the list.

We can achieve this by combining the Where() function with the OfType() method. Let us introduce them one by one and their syntax -

OfType()

is translated as:

OfType()

OfType() method is used to filter the elements of IEnumerable according to the given type. In other words, this method is used to filter a list or sequence source based on whether it has the ability to convert a collection of items to a specific type. If the supplied source is null, an ArgumentNullException is thrown.

grammar

public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult>(this System.Collections.IEnumerable source); 
Copy after login
The translation of

Where()

is:

Where()

Use the Where() method to filter values ​​based on the predicate function. It can also be said that it returns values ​​in a sequence or list based on specified conditions or requirements.

grammar

Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 
Copy after login

Let's create a list containing objects of different data types.

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

We have got a list of elements containing different data types. The output will now contain only negative doubles.

Output: {-2.71, -4.2}
Copy after login

Let us understand

algorithm

Step 1 - Create a list of objects

List<object> list = new List<object>() 
Copy after login

Step 2 - Select the double value from the list.

We can use LINQ's OfType() method to remove double values ​​from the list. After filtering the collection, this function returns only elements of the provided type "double". In our case we want to remove only the double values ​​of the list. Here's how we apply this strategy -

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

Step 3 - From the double values ​​selected in the list, select the negative double value. A filter must be used to filter out negative double values ​​from the collection. This can be achieved using the LINQ Where() method. This method, after applying a filter to the collection, returns only elements that match the filter. In this case, we only want to filter out the negative double values ​​in the collection. Here's how to apply this technique.

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

Step 4 - Finally, use a foreach loop to print the negative double.

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

These are simple steps to get negative numbers from a list. Let's take 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);
      }
   }
}
Copy after login

Output

The output of the above code is -

-2.71 
-4.2 
Copy after login

Note − You may wonder why -2 and -7.5 are not mentioned here. This is because -2 is a negative number, but not a double. And -7.5 is written as "-7.5", which is treated as a string.

in conclusion

In this article, we showed how to use LINQ in C# to find negative double numbers from a list of objects. We used two methods Where() and OfType() to write the code. We briefly discussed the Where() and OfType() methods. The problem statement is explained with an example. After we discussed the algorithm. Finally, the code and output are shown. We hope this article helps you increase your knowledge and understanding of this topic.

The above is the detailed content of C# program to find negative double numbers from a list of objects using 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