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.
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() 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.
public static System.Collections.Generic.IEnumerable<TResult> OfType<TResult>(this System.Collections.IEnumerable source);
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.
Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
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}
We have got a list of elements containing different data types. The output will now contain only negative doubles.
Output: {-2.71, -4.2}
Let us understand
Step 1 - Create a list of objects
List<object> list = new List<object>()
Step 2 - Select the double value from the list.
We can use LINQ's OfType
var doubles = list.OfType<double>();
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);
Step 4 - Finally, use a foreach loop to print the negative double.
foreach (var d in doubles) { Console.WriteLine(d); }
These are simple steps to get negative numbers from a list. Let's take a look at the code.
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); } } }
The output of the above code is -
-2.71 -4.2
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 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!