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

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

LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to query data from various sources including arrays, collections, and databases. It provides SQL-like syntax for data manipulation and filtering, making complex data operations simple and readable.

Problem Statement

Given a list of objects containing different data types, we need to find only the negative double numbers using LINQ. This requires two filtering steps

  • Filter elements that are of double type using OfType()

  • Filter negative values from those doubles using Where()

LINQ Methods Used

OfType() Method

The OfType() method filters elements from an IEnumerable based on a specified type. It returns only elements that can be cast to the specified type.

public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source)

Where() Method

The Where() method filters elements based on a predicate function. It returns elements that satisfy the specified condition.

public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate)

Algorithm

Step 1 Create a list of mixed-type objects

Step 2 Use OfType<double>() to filter only double values

Step 3 Use Where(d => d < 0) to filter negative doubles

Step 4 Display the results using a foreach loop

LINQ Filtering Process Mixed List 3.14, -2.71, "hello" 0, "-7.5", -4.2 OfType<double>() Double Values 3.14, -2.71, -4.2 Where(d < 0) Negative Doubles -2.71, -4.2 Filtered: -2 (int), "-7.5" (string), "hello" (string)

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main(string[] args) {
        // Create a list of mixed-type objects
        List<object> list = new List<object> {
            3.14, -2.71, "hello", 0, "-7.5", "world", -4.2, 'a', -3
        };
        
        Console.WriteLine("Original list contains mixed types:");
        foreach (var item in list) {
            Console.WriteLine($"{item} ({item.GetType().Name})");
        }
        
        // Filter negative double numbers using LINQ
        var negativeDoubles = list.OfType<double>().Where(d => d < 0);
        
        Console.WriteLine("\nNegative double numbers:");
        foreach (var d in negativeDoubles) {
            Console.WriteLine(d);
        }
    }
}

The output of the above code is

Original list contains mixed types:
3.14 (Double)
-2.71 (Double)
hello (String)
0 (Int32)
-7.5 (String)
world (String)
-4.2 (Double)
a (Char)
-3 (Int32)

Negative double numbers:
-2.71
-4.2

Using Method Chaining

The filtering operations can be combined in a single LINQ expression for more concise code

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
    static void Main(string[] args) {
        List<object> list = new List<object> {
            5.8, -1.25, "test", -9.7, 42, -0.5
        };
        
        // Method chaining approach
        var negativeDoubles = list.OfType<double>().Where(d => d < 0);
        
        Console.WriteLine("Negative doubles found:");
        if (negativeDoubles.Any()) {
            foreach (var num in negativeDoubles) {
                Console.WriteLine($"  {num}");
            }
        } else {
            Console.WriteLine("  None found");
        }
        
        // Count negative doubles
        int count = negativeDoubles.Count();
        Console.WriteLine($"Total negative doubles: {count}");
    }
}

The output of the above code is

Negative doubles found:
  -1.25
  -9.7
  -0.5
Total negative doubles: 3

Why Some Values Are Filtered Out

Understanding why certain values don't appear in the result

  • -2 and -3 are int type, not double

  • "-7.5" is a string, not a numeric type

  • 'a' is a char type

  • Only actual double values pass through OfType<double>()

Conclusion

Using LINQ's OfType<double>() and Where() methods, we can efficiently filter negative double numbers from a mixed-type object list. This approach demonstrates LINQ's power in combining type filtering with conditional filtering in a clean, readable manner.

Updated on: 2026-03-17T07:04:36+05:30

596 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements