×

VB.Net Programs

VB.Net program to demonstrate the OfType operator in LINQ query

By Nidhi Last Updated : November 13, 2024

VB.Net – OfType Operator in LINQ Query

In this program, we will use the OfType operator in the LINQ query to get the string elements from the array list and print them on the console screen.

Program/Source Code:

The source code to demonstrate the OfType operator in the LINQ query is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of OfType operator in LINQ query

'VB.NET program to demonstrate the 
'OfType operator in LINQ query.

Imports System
Imports System.IO
Imports System.Linq

Module Module1
    Sub Main()
        Dim lst As New ArrayList()

        lst.Add(101)
        lst.Add("India")
        lst.Add("USA")
        lst.Add(201)

        Dim Result = From s In lst.OfType(Of String)()

        Console.WriteLine("Result: ")
        For Each str As String In Result
            Console.WriteLine(vbTab & str)
        Next
    End Sub
End Module

Output

Result:
        India
        USA
Press any key to continue . . .

Explanation

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

In the Main() function, We created an array list that contains mixed elements that may be integer or string. Here we used the OfType operator to find out the string elements from the array list and then print them on the console screen.

VB.Net LINQ Query Programs »


Comments and Discussions!

Load comments ↻





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