Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Find Integer Numbers from the List of Objects and Sort them Using LINQ
In this article, we will learn how to write a C# program to find integer numbers from a list of objects and sort them using LINQ. LINQ (Language Integrated Query) is one of C#'s powerful features that enables developers to query data from various sources using a SQL-like syntax. It provides a standard approach for data manipulation and sorting, regardless of the data source.
Problem Statement
We need to extract integer numbers from a heterogeneous list containing different data types (strings, integers, characters) and sort them in ascending order. We'll use LINQ's OfType<T>() method to filter integers and OrderBy() method to sort them.
Syntax
Following is the syntax for the OfType<T>() method
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source);
Following is the syntax for the OrderBy() method
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector );
How It Works
Example
The following example demonstrates how to extract and sort integers from a mixed object list
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
// Create a list with mixed data types
List<object> mixedList = new List<object> {
"Tutpoints", 100, "LINQ", 18, "50", 20, 'A', 34
};
// Filter integers and sort them
var sortedIntegers = mixedList.OfType<int>().OrderBy(x => x);
Console.WriteLine("Original list contains mixed types:");
foreach (var item in mixedList) {
Console.WriteLine($"{item} ({item.GetType().Name})");
}
Console.WriteLine("\nSorted integer values:");
foreach (int number in sortedIntegers) {
Console.WriteLine(number);
}
}
}
The output of the above code is
Original list contains mixed types: Tutpoints (String) 100 (Int32) LINQ (String) 18 (Int32) 50 (String) 20 (Int32) A (Char) 34 (Int32) Sorted integer values: 18 20 34 100
Using Method Chaining
You can chain LINQ methods together for more concise code
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
List<object> data = new List<object> {
"Hello", 25, 3.14, 42, "World", 7, 'X', 99
};
// Chain OfType and OrderBy in a single statement
var result = data.OfType<int>().OrderBy(x => x).ToList();
Console.WriteLine("Extracted and sorted integers:");
Console.WriteLine(string.Join(", ", result));
// Also show count
Console.WriteLine($"Total integers found: {result.Count}");
}
}
The output of the above code is
Extracted and sorted integers: 7, 25, 42, 99 Total integers found: 4
Key Methods Used
| Method | Purpose | Returns |
|---|---|---|
OfType<T>() |
Filters elements by specified type | IEnumerable<T> |
OrderBy() |
Sorts elements in ascending order | IOrderedEnumerable<T> |
ToList() |
Converts to List for immediate execution | List<T> |
Conclusion
LINQ's OfType<int>() method efficiently filters integer values from heterogeneous collections, while OrderBy() sorts them in ascending order. This approach provides a clean, readable solution for extracting and organizing specific data types from mixed object collections using method chaining.
