Home >Backend Development >C#.Net Tutorial >C# program to find integers from a list of objects and sort them using LINQ

C# program to find integers from a list of objects and sort them using LINQ

PHPz
PHPzforward
2023-09-07 18:57:12851browse

C# 程序从对象列表中查找整数并使用 LINQ 对它们进行排序

Introduction

In this article, we will learn how to write a C# program to find integers from a list of objects and sort them 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. Language-Integrated Query (sometimes called LINQ) is one of C#'s strengths. 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. It provides a standard syntax for data querying regardless of the data source. 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 integers from a list of objects and sort them using LINQ in C#. Integer doubles must be obtained from a list of objects and then they must be sorted. Therefore, the OfType() method can be used for this operation and the OrderBy() function can be used to sort integers. Let’s review each of them and their syntax -

OfType() method

It is used to filter the elements of IEnumerable according to the specified type. If the supplied source is null, this method throws an ArgumentNullException.

grammar

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

OrderBy() method

Use this technique to sort the elements in a collection in ascending order. This procedure also throws an ArgumentNullException if the supplied source is null.

grammar

public static System.Linq.IOrderedEnumerable
OrderBy<TSource,TKey(thisSystem.Collections.Generic.IEnumerable<TSource> list, Func<TSource,TKey> Selector); 

Let us understand this problem through an example.

Example

Create a list and add elements to it. In this example, we take objects of different data types.

enter

["Tutpoints", 100, "LINQ", 18, “50”, 20, ‘A’, 34] 

For different objects, the OfType() and OrderBy() methods will sort and arrange the integer values ​​in the list. So the output for a given input is

Output

[18, 20, 34,100]

algorithm

Step 1 Create an object list

We first create a list of objects consisting of strings, integers and character combinations.

List<object> list = new List<object> { "Tutpoints", 100, "LINQ", 18, “50”, 20, ‘A’, 34}; 

Step 2 Use OfType() method to find integers

The OfType() method will then be used to filter the list so that only integer values ​​are retained.

var integers = list.OfType<int>(); 

The extension method provided by LINQ is called OfType(). It returns a filtered list of such entries. In this example, the list is filtered to contain only elements of type int. Now only the integer values ​​from the first list appear in the integer variable.

Step 3 Use OrderBy() to sort the integers

We can use the OrderBy() function to sort a sequence of integer values ​​in ascending order.

var sortedIntegers = integers.OrderBy(x => x); 

Another extension function provided by LINQ is OrderBy(). It sorts the list of elements in ascending order based on the given key. In this example, a lambda expression (x => x) is used to specify the key by which the series is sorted. Pass the sequence element (x) to a lambda expression, which then returns the value that will be the sort key.

The integer values ​​in the initial list now appear in the sortedIntegers variable, in ascending order.

Step 4 Print the sorted integers.

Finally, we can use the foreach loop and the Console.WriteLine() method to print out the sorted integer values.

This is a simple algorithm we just read about. Now let's look at a complete C# program that demonstrates how to find integers from a list of objects and sort them using LINQ.

Example

using System;
using System.Collections.Generic;
using System.Linq;
class Program {
   static void Main(string[] args) {
      List<object> list = new List<object> { "Tutpoints", 100, "LINQ", 18, "50", 20, 'A', 34 };
      var integers = list.OfType<int>();
      var sortedIntegers = integers.OrderBy(x => x);
      Console.WriteLine("Sorted integer values are:");
      foreach (int integer in sortedIntegers) {
         Console.WriteLine(integer);
      }
   }
} 

Output

Sorted integer values are:
18
20
34
100

in conclusion

In this article, we show you how to find integers from a list of objects and sort them using LINQ in C#. After removing all values ​​except integer values ​​from the list using OfType() function, we sort the integers using OrderBy() method. Finally, we use the print command to display the sorted integer values ​​to the console. We've seen the algorithm and seen the code. We hope this article helps you enhance your knowledge and understanding of this topic.

The above is the detailed content of C# program to find integers from a list of objects and sort them using LINQ. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Networking in C#Next article:Networking in C#