ADVERTISEMENT
ADVERTISEMENT

C# program to find string values from the list of objects using the Linq OfType() method

Here, we are going to learn how to find string values from the list of objects using the Linq OfType() method in C#?
Submitted by Nidhi, on August 28, 2020

Here we will find the string values from the list of objects. The OfType() method is used to check the type of object data. Here we check string type data from the list and print them on the console screen.

Program:

The source code to find string values from the list of objects using OfType() method of Linq, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# Program to find string values from the list
//of objects using the Linq OfType() method.

using System;
using System.Linq;
using System.Collections.Generic;

class Demo
{
    static void Main(string[] args)
    {
        List<object> objectList = new List<object>()
        {
  101, "Amit", 102,"Joseph", 10.3, "RK Verma", 104, "Sanjay Shukla", 10.5F, "Pramod Pandey"
        };

        List<string> result = objectList.OfType<string>().ToList();

        foreach (string str in result)
        {
            Console.WriteLine(str + " ");
        } 
    }
}

Output:

Amit
Joseph
RK Verma
Sanjay Shukla
Pramod Pandey
Press any key to continue . . .

Explanation:

In the above program, we created a list of objects that contains different types of values then we find string values using OfType() method using the below code.

List<string> result = objectList.OfType<string>().ToList();

Then we printed filtered string values on the console screen.

C# LINQ Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.