| by Arround The Web | No comments

OfType Filtering Operator in C# LINQ

If you want to get the data from the data source or sequence of a particular type, we can use the OfType operator. In C#, the Language Integrated Query (LINQ) provides the filtering mechanisms in which a particular type of data is selected using the OfType filtering operator. Let’s see how to select specific types like integer, string, double, class, etc., from the existing data source (array list) in this guide. Let’s also see how to use this operator in the query syntax as well as in the method syntax.

OfType Filtering Operator

OfType<Type>() is used to select the elements of specified type from the given data source. The elements that don’t match the specified type will be ignored.

Query Syntax:

Let’s see how to use the “select” operator using query. As we know, the “select” projection operator is used to get the elements from the specified data source. While iterating the elements that are present in the data source, we need to specify the OfType filtering operator along with the type.

from iterator in Data_Source.OfType<Type>()
select iterator;

Here:

  1. The Data_Source can be the list that holds the data.
  2. The iterator is used to fetch the elements from the specified Data_Source.

Method Syntax:

In this scenario, we can directly specify the type by specifying the data source.

Data_Source.OfType<Type>();

Example 1: Get the String Type Elements

Let’s create an array list that holds five elements. Using the OfType operator, get the elements of the string type.

using System;
using System.Linq;
using System.Collections;
public class Multiple_Types
{
    public static void Main()
    {
        // ArrayList creation
        ArrayList data1 = new ArrayList();
       
        // Add elements to the Arraylist.
        data1.Add("Lilly");
        data1.Add(120);
        data1.Add("Jasmine");
        data1.Add(45.890);
        data1.Add(100);
       
        // Get only string Type elements from the above ArrayList.
        var string_type = from i in data1.OfType()
                          select i;
 
        foreach(var result1 in string_type)
        {
            Console.WriteLine(result1);
        }
       
       
    }
}

Output:


1. First, we create the Multiple_Types class. Inside the main method, the data1 ArrayList is created with five elements of different types.

2. In Line #21, we use the OfType operator that selects only the elements of string type. After that, the “foreach” loop is utilized to display the elements that are selected by the query.

Example 2: Get the Integer Type Elements

Utilize the previous code (Example 1). Just change the specified type in the OfType operator from string to int.

// Get only integer Type elements from the above ArrayList.
        var int_type = from i in data1.OfType()
                          select i;
 
        foreach(var result1 in int_type)
        {
            Console.WriteLine(result1);
        }

Output: 

In Line #21, we use the OfType operator that selects only the elements of the integer type and stores them in the int_type variable. After that, we iterate this variable inside the “foreach” loop to display the elements.

Example 3: Get the Double Type Elements

Utilize the previous code (Example 2). Just change the specified type in the OfType operator from int to double.

// Get only double Type elements from the above ArrayList.
        var double_type = from i in data1.OfType()
                          select i;
 
        foreach(var result1 in double_type)
        {
            Console.WriteLine(result1);
        }

Output:

There is only one element which is of double type.

In Line #21, we use the OfType operator that selects only the elements of the double type and stores them in the double_type variable. After that, we iterate this variable inside the “foreach” loop to display the elements.

Example 4: Get the Class Instance Elements

Create a class named “Building” with two properties: address and cost. Again, create a main class that holds an array list with some records. Use the <Building> OfType operator to filter the building-related records

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

public class Types
{
    public static void Main()
    {
        // ArrayList creation
        ArrayList data1 = new ArrayList();
       
        // Add elements to the Arraylist.
        data1.Add("Hello LinuxHint");
        data1.Add(new Building() {address = "India", cost = 500});
        data1.Add(new Building() {address = "USA", cost = 2500});
        data1.Add(new Building() {address = "UK", cost = 3000});
       
       
        // Get only Building_type elements.
        var Building_type = from i in data1.OfType()
                            select i;
 
        foreach(var result1 in Building_type)
        {
            Console.WriteLine(result1.address +" --- "+ result1.cost);
        }
       
       
    }
}  
    public class Building {
 
    public string address {get;set;}
    public int cost {get;set;}
}

Output: 

Explanation:

1. First, we create a “Building” class with two properties.

2. Next, we add the following elements to the data1 array list in the main class which is “Types”.

3. Finally, we specify the OfType as building in the Query and use a “foreach” loop to get the building address and building cost from the Building_type variable.

Example 5: Get All Types Using the Method Syntax

Create the data1 ArrayList with all types and get the types separately using the OfType operator with the method syntax.

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

public class Types
{
    public static void Main()
    {
        // ArrayList creation
        ArrayList data1 = new ArrayList();
       
        // Add elements to the Arraylist.
        data1.Add("Hello LinuxHint");
        data1.Add("C#");
        data1.Add(new Building() {address = "India", cost = 500});
        data1.Add(new Building() {address = "USA", cost = 2500});
        data1.Add(new Building() {address = "UK", cost = 3000});
        data1.Add(678.90);
        data1.Add(45);
       
       
        // Get only Building_type elements.
        var Building_type = data1.OfType();

        Console.WriteLine("--------Building--------");
        foreach(var result1 in Building_type)
        {
            Console.WriteLine(result1.address +" "+ result1.cost);
        }
       
       
         Console.WriteLine("--------String--------");
        // Get only string type elements.
        var string_type = data1.OfType();
 
        foreach(var result2 in string_type)
        {
            Console.WriteLine(result2);
        }
       
       
        Console.WriteLine("--------Integer--------");
        // Get only integer type elements.
        var int_type = data1.OfType();
 
        foreach(var result3 in int_type)
        {
            Console.WriteLine(result3);
        }
       
        Console.WriteLine("--------Double--------");
        // Get only double type elements.
        var double_type = data1.OfType();
 
        foreach(var result4 in double_type)
        {
            Console.WriteLine(result4);
        }
    }
}  
    public class Building {
    public string address {get;set;}
    public int cost {get;set;}
}

Output:

Explanation:

1. First, we create a “Building” class with two properties.

2. Then, we add the elements of different types including the “Building” type.

3. Get the Building_type elements only. There are three elements here.

4. Get the string type elements only. There are only two elements here.

5. Get the integer type elements only. There is only one element here.

6. Get the double type elements only. There is only one element here

Conclusion

In C# LINQ, the OfType operator is a filtering operator which is used to select the specified type of elements from the given data source. The elements that don’t match the specified type are ignored. We learned the five different examples to get the different types in each scenario. Lastly, we utilized this operator in the method syntax for all possible types. The types that we specified are int for Integer, string for Strings, double for Double values and class name for Class types.

Share Button

Source: linuxhint.com

Leave a Reply