| by Arround The Web | No comments

LINQ OfType() Method

Suppose In a Datasource there are elements with different data types like string,Integer, double etc., and you need to get only a particular type, you should know about OfType() method available in C#. The data source can be an ArrayList.

OfType()

OfType() method in LINQ is used to eliminate the unnecessary data type elements and return only elements of a single data type.

Syntax:

input_source.OfType<datatype>()

Where:

  1. input_source can be an Arraylist.
  2. datatype is the type we will return like string,int,double etc.

Example 1:

Here, we will create a data source named Array List and it has three data type elements. So, we will extract the elements only of string type.

The syntax should be:

input_source.OfType();
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;  

class Linuxhint {

  public static void Main()
    {
        //create an array list that has multiple datatype elements.
        var my_arraylist=new ArrayList(){1,"Linuxhint","java",4.56,90.5355,6};

        //display the ArrayList
        foreach (var result in my_arraylist){
Console.WriteLine(result);
        }
Console.WriteLine("------------------------");

//return only string type elements
var string_types=my_arraylist.OfType();

//display
foreach (var result in string_types){
Console.WriteLine(result);
        }
    }
}

Output:

Explanation:

Create an array list named – my_arraylist.

Return only strings.

Display the result using a foreach loop.

Example 2:

Here, we will create a data source named Array List and it has three data type elements. We will extract the elements only of integer type.

The syntax should be:

input_source.OfType();
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;  

class Linuxhint {

  public static void Main()
    {
        //create an array list that has multiple datatype elements.
        var my_arraylist=new ArrayList(){1,"Linuxhint","java",4.56,90.5355,6};

        //display the ArrayList
        foreach (var result in my_arraylist){
Console.WriteLine(result);
        }
Console.WriteLine("------------------------");

//return only integer type elements
var int_types=my_arraylist.OfType();

//display
foreach (var result in int_types){
Console.WriteLine(result);
        }
    }
}

Output:

Explanation:

Create an array list named – my_arraylist.

Return only integers.

Display the result using a foreach loop.

Example 3:

Here, we will create a data source named Array List and it has three data type elements. We will extract the elements only of double type.

The syntax should be:

input_source.OfType();
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;  

class Linuxhint {

  public static void Main()
    {
        //create an array list that has multiple datatype elements.
        var my_arraylist=new ArrayList(){1,"Linuxhint","java",4.56,90.5355,6};

        //display the ArrayList
        foreach (var result in my_arraylist){
Console.WriteLine(result);
        }
Console.WriteLine("------------------------");

//return only double type elements
var double_types=my_arraylist.OfType();

//display
foreach (var result in double_types){
Console.WriteLine(result);
        }
    }
}

Output:

Explanation:

Create an array list named – my_arraylist.

Return only double type values.

Display the result using a foreach loop.

Conclusion

In this tutorial, we discussed the OfType() method. OfType() method in LINQ is used to eliminate the unnecessary data type elements and return only elements of a single data type. In projects, if you need only particular data types like strings, integers or double values, you can specify int to return only integer values, string to return string values and double to return double values.

Share Button

Source: linuxhint.com

Leave a Reply