HomeLinuxOfType Filtering Operator in C# LINQ

OfType Filtering Operator in C# LINQ


If you wish to get the information from the information supply or sequence of a selected sort, we are able to use the OfType operator. In C#, the Language Built-in Question (LINQ) supplies the filtering mechanisms by which a selected sort of knowledge is chosen utilizing the OfType filtering operator. Let’s see learn how to choose particular varieties like integer, string, double, class, and so on., from the prevailing knowledge supply (array record) on this information. Let’s additionally see learn how to use this operator within the question syntax in addition to within the technique syntax.

OfType Filtering Operator

OfType<Sort>() is used to pick out the weather of specified sort from the given knowledge supply. The weather that don’t match the desired sort shall be ignored.

Question Syntax:

Let’s see learn how to use the “choose” operator utilizing question. As we all know, the “choose” projection operator is used to get the weather from the desired knowledge supply. Whereas iterating the weather which might be current within the knowledge supply, we have to specify the OfType filtering operator together with the sort.

from iterator in Data_Source.OfType<Sort>()
choose iterator;

Right here:

  1. The Data_Source might be the record that holds the information.
  2. The iterator is used to fetch the weather from the desired Data_Source.

Technique Syntax:

On this state of affairs, we are able to immediately specify the sort by specifying the information supply.

Data_Source.OfType<Sort>();

Instance 1: Get the String Sort Components

Let’s create an array record that holds 5 parts. Utilizing the OfType operator, get the weather of the string sort.

utilizing System;
utilizing System.Linq;
utilizing System.Collections;
public class Multiple_Types
{
    public static void Foremost()
    {
        // ArrayList creation
        ArrayList data1 = new ArrayList();
       
        // Add parts to the Arraylist.
        data1.Add(“Lilly”);
        data1.Add(120);
        data1.Add(“Jasmine”);
        data1.Add(45.890);
        data1.Add(100);
       
        // Get solely string Sort parts from the above ArrayList.
        var string_type = from i in data1.OfType()
                          choose i;
 
        foreach(var result1 in string_type)
        {
            Console.WriteLine(result1);
        }
       
       
    }
}

Output:


1. First, we create the Multiple_Types class. Inside the principle technique, the data1 ArrayList is created with 5 parts of various varieties.

2. In Line #21, we use the OfType operator that selects solely the weather of string sort. After that, the “foreach” loop is utilized to show the weather which might be chosen by the question.

Instance 2: Get the Integer Sort Components

Make the most of the earlier code (Instance 1). Simply change the desired sort within the OfType operator from string to int.

// Get solely integer Sort parts from the above ArrayList.
        var int_type = from i in data1.OfType()
                          choose i;
 
        foreach(var result1 in int_type)
        {
            Console.WriteLine(result1);
        }

Output: 

In Line #21, we use the OfType operator that selects solely the weather of the integer sort and shops them within the int_type variable. After that, we iterate this variable contained in the “foreach” loop to show the weather.

Instance 3: Get the Double Sort Components

Make the most of the earlier code (Instance 2). Simply change the desired sort within the OfType operator from int to double.

// Get solely double Sort parts from the above ArrayList.
        var double_type = from i in data1.OfType()
                          choose i;
 
        foreach(var result1 in double_type)
        {
            Console.WriteLine(result1);
        }

Output:

There is just one component which is of double sort.

In Line #21, we use the OfType operator that selects solely the weather of the double sort and shops them within the double_type variable. After that, we iterate this variable contained in the “foreach” loop to show the weather.

Instance 4: Get the Class Occasion Components

Create a category named “Constructing” with two properties: deal with and price. Once more, create a foremost class that holds an array record with some information. Use the <Constructing> OfType operator to filter the building-related information

utilizing System;
utilizing System.Linq;
utilizing System.Collections;

public class Sorts
{
    public static void Foremost()
    {
        // ArrayList creation
        ArrayList data1 = new ArrayList();
       
        // Add parts to the Arraylist.
        data1.Add(“Hi there LinuxHint”);
        data1.Add(new Constructing() {deal with = “India”, value = 500});
        data1.Add(new Constructing() {deal with = “USA”, value = 2500});
        data1.Add(new Constructing() {deal with = “UK”, value = 3000});
       
       
        // Get solely Building_type parts.
        var Building_type = from i in data1.OfType()
                            choose i;
 
        foreach(var result1 in Building_type)
        {
            Console.WriteLine(result1.deal with +” — “+ result1.value);
        }
       
       
    }
}  
    public class Constructing {
 
    public string deal with {get;set;}
    public int value {get;set;}
}

Output: 

Clarification:

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

2. Subsequent, we add the next parts to the data1 array record in the principle class which is “Sorts”.

3. Lastly, we specify the OfType as constructing within the Question and use a “foreach” loop to get the constructing deal with and constructing value from the Building_type variable.

Instance 5: Get All Sorts Utilizing the Technique Syntax

Create the data1 ArrayList with all kinds and get the kinds individually utilizing the OfType operator with the strategy syntax.

utilizing System;
utilizing System.Linq;
utilizing System.Collections;

public class Sorts
{
    public static void Foremost()
    {
        // ArrayList creation
        ArrayList data1 = new ArrayList();
       
        // Add parts to the Arraylist.
        data1.Add(“Hi there LinuxHint”);
        data1.Add(“C#”);
        data1.Add(new Constructing() {deal with = “India”, value = 500});
        data1.Add(new Constructing() {deal with = “USA”, value = 2500});
        data1.Add(new Constructing() {deal with = “UK”, value = 3000});
        data1.Add(678.90);
        data1.Add(45);
       
       
        // Get solely Building_type parts.
        var Building_type = data1.OfType();

        Console.WriteLine(“——–Constructing——–“);
        foreach(var result1 in Building_type)
        {
            Console.WriteLine(result1.deal with +” “+ result1.value);
        }
       
       
         Console.WriteLine(“——–String——–“);
        // Get solely string sort parts.
        var string_type = data1.OfType();
 
        foreach(var result2 in string_type)
        {
            Console.WriteLine(result2);
        }
       
       
        Console.WriteLine(“——–Integer——–“);
        // Get solely integer sort parts.
        var int_type = data1.OfType();
 
        foreach(var result3 in int_type)
        {
            Console.WriteLine(result3);
        }
       
        Console.WriteLine(“——–Double——–“);
        // Get solely double sort parts.
        var double_type = data1.OfType();
 
        foreach(var result4 in double_type)
        {
            Console.WriteLine(result4);
        }
    }
}  
    public class Constructing {
    public string deal with {get;set;}
    public int value {get;set;}
}

Output:

Clarification:

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

2. Then, we add the weather of various varieties together with the “Constructing” sort.

3. Get the Building_type parts solely. There are three parts right here.

4. Get the string sort parts solely. There are solely two parts right here.

5. Get the integer sort parts solely. There is just one component right here.

6. Get the double sort parts solely. There is just one component right here

Conclusion

In C# LINQ, the OfType operator is a filtering operator which is used to pick out the desired sort of parts from the given knowledge supply. The weather that don’t match the desired sort are ignored. We realized the 5 totally different examples to get the differing types in every state of affairs. Lastly, we utilized this operator within the technique syntax for all potential varieties. The categories that we specified are int for Integer, string for Strings, double for Double values and sophistication title for Class varieties.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments