VB.Net program to get distinct elements from the list using LINQ query

Here, we are going to learn how to get distinct elements from the list using LINQ query in VB.Net?
Submitted by Nidhi, on February 02, 2021 [Last updated : March 08, 2023]

VB.Net – Distinct elements from the list using LINQ query

In this program, we will get distinct elements from the list of strings using the LINQ query.

Program/Source Code:

The source code to get distinct elements from the list using the LINQ query is given below. The given program is compiled and executed successfully.

VB.Net code to get the distinct elements from the list using LINQ query

'VB.NET program to get distinct elements 
'from a list using LINQ query.

Module Module1
    Sub Main()
        Dim strList = New List(Of String) From {"India", "US", "UK", "India", "UK"}

        Dim distinctValues = From s In strList
                             Select s Distinct

        Console.WriteLine("Distinct Elements:")
        For i = 0 To distinctValues.Count() - 1 Step 1
            Console.Write(distinctValues.ElementAt(i) & " ")
        Next
        Console.WriteLine()
    End Sub
End Module

Output

Distinct Elements:
India US UK
Press any key to continue . . .

Explanation

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

In the Main() function, We get the distinct elements from the list of strings using LINQ query and then print distinct elements on the console screen.

VB.Net LINQ Query Programs »





Comments and Discussions!

Load comments ↻





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