×

VB.Net Programs

VB.Net program to demonstrate the ThenByDescending() LINQ extension method

By Nidhi Last Updated : November 13, 2024

VB.Net – ThenByDescending() LINQ Extension Method

In this program, we will use OrderBy() and ThenByDescending() extension methods to sort the employee list, here ThenBy() method is used to sort the list on another field in descending order after OrderBy() and print them on the console screen.

Program/Source Code:

The source code to demonstrate the ThenByDescending() LINQ extension method is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of ThenByDescending() LINQ Extension Method

'VB.NET program to demonstrate the 
'ThenByDescending() LINQ extension method.

Imports System
Imports System.IO
Imports System.Linq

Module Module1
    Public Class Employee
        Public id As Integer
        Public name As String
        Public age As Integer
    End Class

    Sub Main()
        Dim empList = New List(Of Employee) From
        {
            New Employee() With {.id = 101, .name = "Amit", .age = 21},
            New Employee() With {.id = 102, .name = "Arun", .age = 22},
            New Employee() With {.id = 103, .name = "Aman", .age = 23},
            New Employee() With {.id = 104, .name = "Amar", .age = 24},
            New Employee() With {.id = 105, .name = "Akki", .age = 25}
        }

        Dim Emps = empList.OrderBy(Function(e) e.name).ThenByDescending(Function(e) e.age)

        Console.WriteLine("Employees detail: ")
        For Each emp As Employee In Emps
            Console.WriteLine(emp.id & " " & emp.name & " " & emp.age)
        Next
    End Sub
End Module

Output

Employees detail:
105 Akki 25
103 Aman 23
104 Amar 24
101 Amit 21
102 Arun 22
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a class Employee and the Main() function. The Main() function is the entry point for the program.

The Employee class contains data member "id", "name" and "age". In the Main() function, We created the list of employees. Here, we used OrderBy() and ThenByDescending() LINQ method to sort the employee's list based on their names and age and print the employee's detail on the console screen.

VB.Net LINQ Query Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.