Jump to content

OfType Filtering Operator in C# LINQ


Linux Hint

Recommended Posts

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:

c-sharp-linq-filtering-operator-oftype-0
1. First, we create the Multiple_Types class. Inside the main method, the data1 ArrayList is created with five elements of different types.
c-sharp-linq-filtering-operator-oftype-0
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.
c-sharp-linq-filtering-operator-oftype-0

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: 

c-sharp-linq-filtering-operator-oftype-0

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.

c-sharp-linq-filtering-operator-oftype-0

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:

c-sharp-linq-filtering-operator-oftype-0

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.

c-sharp-linq-filtering-operator-oftype-0

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: 

c-sharp-linq-filtering-operator-oftype-0

Explanation:

1. First, we create a “Building” class with two properties.
c-sharp-linq-filtering-operator-oftype-0
2. Next, we add the following elements to the data1 array list in the main class which is “Types”.
c-sharp-linq-filtering-operator-oftype-1
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.
c-sharp-linq-filtering-operator-oftype-1

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:

c-sharp-linq-filtering-operator-oftype-1

Explanation:

1. First, we create a “Building” class with two properties.
c-sharp-linq-filtering-operator-oftype-1
2. Then, we add the elements of different types including the “Building” type.
c-sharp-linq-filtering-operator-oftype-1
3. Get the Building_type elements only. There are three elements here.
c-sharp-linq-filtering-operator-oftype-1
4. Get the string type elements only. There are only two elements here.
c-sharp-linq-filtering-operator-oftype-1
5. Get the integer type elements only. There is only one element here.
c-sharp-linq-filtering-operator-oftype-1
6. Get the double type elements only. There is only one element herec-sharp-linq-filtering-operator-oftype-1

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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...